It is narrated from Sayyiduna Ibn Umar (may Allah be pleased with them both) that the Messenger of Allah (peace and blessings be upon him) said: "Whoever forgets a prayer and remembers it only when he is (in prayer) with the imam, let him complete that prayer with the imam; then, when he has finished that prayer, let him first perform the prayer he had forgotten, and after that repeat the prayer he performed with the imam."
Hadith Referenceإتحاف الخيرة المهرة بزوائد المسانيد العشرة / كتاب افتتاح الصلاة / 1418
Hadith Gradingمحدثین:سكت عنه البوصيري، إسناده ضعيف؛ والصحيح وقفه على ابن عمر، تفرد الترجماني برفعه
Hadith Takhrij«قال البيهقي: تفرد أبو إبراهيم الترجماني برفعه، والصحيح أنه من قول ابن عمر موقوفًا؛ وصحح الموقوف أبو زرعة والدارقطني؛ سعيد بن عبد الرحمن الجمحي: صدوق له أوهام (التقريب)»