Al-Mughira b. Shu'ba reported God’s Messenger as saying, “When an imam stands up at the end of two rak'as, if he remembers before standing straight up he should sit down, but if he stands straight up he must not sit down, but perform the two prostrations of forgetfulness.”
‘Imran b. Husain said that the Prophet led them in prayer and forgot something, so he made two prostrations, then uttered the shahada, then gave the salutation.
Tirmidhi transmitted it and said this is a hasan gharib tradition.
Imran b. Husain said that God’s Messenger prayed the afternoon prayer and gave the salutation at the end of three rak'as, then went into his house. A man called al-Khirbaq who had long arms got up and went to him, and addressing him mentioned to him what he had done. He came out angrily trailing his cloak, and when he came to the people he asked, “Is this man telling the truth?” When they told him that he was, he prayed a rak‘a, then gave the salutation, then made two prostrations, then gave the salutation.
‘Abdallah ibn Buhaina said that the Prophet led them in the noon prayer, and when he stood up at the end of the first two rak'as and did not sit, the people stood up along with him. When he finished the prayer and the people expected him to give the salutation, he said “God is most great" while sitting and made two prostrations before giving the salutation. Then he gave it.