All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
The scholars provided different opinions regarding a person who remembers a missed prayer while praying a present one. The Hanbali scholars are of the opinion that such a person should complete his prayer provided that there is sufficient time to make up the missed prayer and then repeat the present prayer in order to maintain sequence, which is obligatory from their viewpoint. The Maaliki scholars are of the opinion that such a person has to end the prayer, whether he is an Imaam or praying alone. However, when he violates sequence and continues in prayer, his prayer is valid, as they (the Maaliki scholars) do not consider maintaining sequence a condition. Then it is recommended for him to repeat the prayer as long as it is the part of the time of the prayer when one is not required to immediately pray. As for the ruling on the prayer of those praying behind him, the scholars provided two different opinions. The preponderant opinion, however, is that they are not required to make up the prayer, as the Imaam's prayer was perfect in itself. This is the opinion of Taawoos, Al-Hasan, Ash-Shaafi‘i and Abu Thawr .
Consequently, the questioner's prayer is valid as well as that of those who prayed behind him.