Being human entails making mistakes, even in important duties such as prayer. Not even the best of all human beings, the Prophet Muhammad (may the peace and blessings of Allah be upon him) was exempt from this, as is mentioned in the following narration of 'Abdullāh ibn Mas'ood (may Allah be pleased with him):
"The Prophet (may the peace and blessings of Allah be upon him) prayed either an extra addition, or missed an essential part of the prayer (one of the narrators of the hadeeth was in doubt which). After saying the tasleem (saying ‘salaam’ to the right then the left to conclude the prayer) it was said to him, 'O Prophet of Allah, has there been a new change in [the way of performing] the prayer?' He replied, 'Why [would you say that]?' They responded, 'You prayed in such and such manner.' So he bent his legs and faced towards the qiblah and performed two prostrations then performed [another] tasleem.
After facing towards us, he said, 'If something new had been introduced regarding the prayers, I would have informed you about it. To the contrary, I am but a human like you—I forget as you forget, so if I forget, remind and inform me. If any of you doubts during his prayer then he should try to discern the more certain and correct case, then complete the prayer based upon it, then perform tasleem, followed by two prostrations.'" [al-Bukhārī: 401]
The prostration of forgetfulness (known in Arabic as sujood as-sahw) is required whenever a person inadvertently adds to, misses, or is in doubt regarding parts of the prayer. It only applies to those parts of the prayer that are obligatory, and doesn't apply to mistakes in the voluntary or recommended acts of the prayer.
You should perform two prostrations for forgetfulness; prostrating and sitting between the two prostrations, just as you would in the regular prayer. Finally, conclude the prayer with the tasleem, by saying: assalaamu 'alaykum warahmatullaah to the right, then to the left.
There are two different times of the prostration:
The scholars agreed that whether you perform the prostration before or after the tasleem, it is valid. However, the preferred method depends on the circumstances of the mistake. For this reason, we have prepared a set of three flow charts to make it easy to understand what should be done in the different situations. There are three basic kinds of mistakes, and a flow chart for each one if you click the links:
In each of the above flow charts, it is important to be familiar with the pillars and obligations of the prayer, which are as follows:
A pillar of the prayer is something that would make the prayer invalid if it was missed. The pillars of the prayer include: the opening takbeer, reciting Surah al-Faatihah, the bowing position, standing up straight from the bowing, prostration, rising from prostration to the sitting position, the final tashahhud, and the tasleem.
An obligation of the prayer is something that you have to do, but would not make the prayer invalid if it was missed. Obligations of the prayer include: saying Allaahu Akbar as you move from position to position, saying "sami’a Allahu liman hamidah" for the one who is leading the prayer or the one praying alone, saying "Subhaana rabbiy al-‘azeem" at least once while bowing, saying "Subhaana rabbiy al-a’laa" at least once while prostrating, saying "Rabb-ighfir li" between the two prostrations, and the middle tashahhud.
Note that when following an imām in prayer, you must follow his prostrations of forgetfulness, even if you didn't make a mistake. On the other hand, if you make a mistake in the prayer that you pray behind the imām, then there is no need to prostrate, as his prayer makes up for the mistake that you made. If, however, you make a mistake in a part of the prayer in which you are not following him, the normal rules for mistakes apply.
The evidence for this is the following narration of Abū Hurayrah (may Allah be pleased with him) that the Prophet (may the peace and blessings of Allah be upon him) said:
"They lead you in prayer; if they do it properly then it is for you and for them, but if they make a mistake then it is for you and against them." [al-Bukārī: 694]