Answered by Shaykh Amjad Rasheed
Sometimes the imam makes a mistake and closes the prayer with salams before the end of the prayer, then once he remembers he rises, says “Allahu akbar” and prays the missed rak’a. Is the prayer valid?
If one absentmindedly closes the prayer with salams before completing the number of rak’as originally intended, then
a) if there is a brief interval before one recalls it, one rises and prays the remaining rak’as. It is praiseworthy to also prostrate for forgetfulness before closing with salams.
b) if there was a lengthy interval before one recalls it, it is obligatory to repeat the prayer if was fard, and it is praiseworthy to repeat it if it was sunnah.
The criteria for a lengthy interval is the time it takes to perform two short rak’as, (approximately 1 minute). Likewise, if during that interval something that invalidates the prayer occurs, such as coming in contact with filth or much movement, then it is obligatory to repeat the fard prayer.
– Amjad Rasheed
(Translated by Sr Lida Kahi)