[Machine] Whoever forgets a prayer from his prayers, and he only remembers it while behind the imam, then when the imam says the salutation, he should perform the prayer that he forgot, and then perform another prayer after the other prayer. Ibn Wahb, Malik, Layth bin Sa`d, and Yahya bin Abdullah bin Salim said the same. The Sheikh, may Allah have mercy on him, said that repeating the prayer that he prayed with the imam according to Ash-Shafi'i, may Allah have mercy on him, is recommended, not obligatory. And Allah knows best.
مَنْ نَسِيَ صَلَاةً مِنْ صَلَوَاتِهِ فَلَمْ يَذْكُرْهَا إِلَّا وَهُوَ وَرَاءَ الْإِمَامِ فَإِذَا سَلَّمَ الْإِمَامُ فَلْيُصَلِّ الصَّلَاةَ الَّتِي نَسِيَهَا ثُمَّ لِيُصَلِّ بَعْدُ الصَّلَاةَ الْأُخْرَى قَالَ ابْنُ وَهْبٍ وَقَالَ مَالِكٌ وَاللَّيْثُ بْنُ سَعْدٍ وَيَحْيَى بْنُ عَبْدِ اللهِ بْنِ سَالِمٍ مِثْلَهُ قَالَ الشَّيْخُ رَحِمَهُ اللهُ وَإِعَادَةُ الصَّلَاةِ الَّتِي صَلَّاهَا مَعَ الْإِمَامِ عِنْدَ الشَّافِعِيِّ رَحِمَهُ اللهُ اسْتِحْبَابٌ لَا إِيجَابٌ وَاللهُ أَعْلَمُ