Anas bin Malik reported,

I was with the Prophet ﷺ when a man came to him and said, "Messenger of Allah, I have committed an offense that requires a prescribed punishment, so carry it out on me."The Prophet ﷺ did not ask him about it. Then the time for prayer came, and the man prayed with the Prophet ﷺ. When the Prophet ﷺ finished the prayer, the man stood up and said, "Messenger of Allah, I have committed an offense that requires a prescribed punishment, so apply to me what is in the Book of Allah."The Prophet ﷺ asked, "Did you not just pray with us?"The man replied, "Yes."The Prophet ﷺ said, "Then Allah has forgiven you your sin," or he said, "your prescribed punishment."

Bukhari 6823

Sahih

حدثنا عبد القدوس بن محمد حدثني عمرو بن عاصم الكلابي حدثنا همام بن يحيى حدثنا إسحاق بن عبد الله بن أبي طلحة

عن أنس بن مالك رضي الله عنه قال

كنت عند النبي ﷺ فجاءه رجل فقال يا رسول الله إني أصبت حدا فأقمه علي قال ولم يسأله عنه قال وحضرت الصلاة فصلى مع النبي ﷺ فلما قضى النبي ﷺ الصلاة قام إليه الرجل فقال يا رسول الله إني أصبت حدا فأقم في كتاب الله قال أليس قد صليت معنا قال نعم قال فإن الله قد غفر لك ذنبك أو قال حدك

البخاري 6823صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Bukhari
Book
Legal punishments
Chapter
If someone confesses to a hadd offense but does not specify it, may the imam conceal it for him
Numbering
Darussalam 6823
Bukhari 6823