Yahya related to me from Malik from Nafi that Abdullah ibn Umarused to say, "Someone who only remembers that he has forgotten aprayer when he is praying the next prayer behind an imam, should praythe prayer he has forgotten after the imam has said the taslim, andthen pray the other one again."
USC-MSA web (English) reference: Book 9, Hadith 80
Arabic reference: Book 9, Hadith 411