Abu Saeed Khudri reported,

The Messenger of Allah ﷺ said, "When one of you is uncertain about his prayer, let him cast aside the doubt and build upon what he is certain of. Once he is sure he has completed the prayer, let him perform two prostrations. If his prayer was already complete, the extra unit counts as a voluntary prayer, and if it was deficient, the unit completes his prayer, while the two prostrations rub the nose of Satan in the dust."

Ibn Majah 1210

Sahih

حدثنا أبو كريب حدثنا أبو خالد الأحمر عن ابن عجلان عن زيد بن أسلم عن عطاء بن يسار

عن أبي سعيد الخدري قال

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

ابن ماجه 1210صحيح
Contents
Reference
Commentary
Ayat
Collection
Sunan Ibn Majah
Book
Establishing prayer and the sunnah regarding it
Chapter
Whoever is uncertain in his prayer and returns to what is certain
Numbering
Darussalam 1210
Ibn Majah 1210