Yahya related to me from Malik from Nafi that Abdullah ibn Umar used to say, "Someone who only remembers that he has forgotten a prayer when he is praying the next prayer behind an imam, should pray the prayer he has forgotten after the imam has said the taslim, and then pray the other one again."
أَنَّ عَبْدَ اللَّهِ بْنَ عُمَرَ كَانَ يَقُولُ مَنْ نَسِيَ صَلاَةً فَلَمْ يَذْكُرْهَا إِلاَّ وَهُوَ مَعَ الإِمَامِ فَإِذَا سَلَّمَ الإِمَامُ فَلْيُصَلِّ الصَّلاَةَ الَّتِي نَسِيَ ثُمَّ لِيُصَلِّ بَعْدَهَا الأُخْرَى