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 it, let him perform two prostrations. If his prayer was already complete, the extra unit and the two prostrations count as a voluntary act, and if it was deficient, the unit completes his prayer and the two prostrations rub Satan's nose in the dust."
حدثنا محمد بن العلاء حدثنا أبو خالد عن ابن عجلان عن زيد بن أسلم عن عطاء بن يسار
قال رسول الله ﷺ إذا شك أحدكم في صلاته فليلق الشك وليبن على اليقين فإذا استيقن التمام سجد سجدتين فإن كانت صلاته تامة كانت الركعة نافلة والسجدتان وإن كانت ناقصة كانت الركعة تماما لصلاته وكانت السجدتان مرغمتي الشيطان
Reference. Narrated by Muslim in the Book of Mosques, Chapter on Forgetfulness in Prayer and the Sujud for It, the Prayer and the Sujud for It, hadith no. 571, through this chain. Ibn Majah also narrated it, hadith no. 1210, from Muhammad bin Ala, through this chain, from Zayd bin Aslam, through this chain. Ibn Majah narrated it, hadith no. 10.