The Messenger of Allah ﷺ said, "When the imam says, 'Allah hears whoever praises Him,' say, 'O Allah, our Lord, to You belongs all praise.' Whoever's words coincide with the words of the angels will have his past sins forgiven."
حدثنا عبد الله بن مسلمة عن مالك عن سمي عن أبي صالح السمان
أن رسول الله ﷺ قال إذا قال الإمام سمع الله لمن حمده فقولوا اللهم ربنا لك الحمد فإنه من وافق قوله قول الملائكة غفر له ما تقدم من ذنبه
Bukhari narrated it in Adhan, chapter on the merit of "O Allah, our Lord, to You belongs praise," hadith no. 796, and Muslim in Salah, chapter on tasmi, tahmid, and tamin, hadith no. 409, from the hadith of Malik through this chain. It is also found in Muwatta (Yahya), 1/88, and (Qanabi), p. 142.