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 is His slave and Messenger. We ask Allaah to exalt his mention as well as that of his family and all his companions.
If the person led in prayer moved forward and prayed ahead of the Imaam for a sound and valid excuse, then his prayer is correct and he does not have to make it up. Shaykh Al-Islam Ibn Taymiyyah after quoting the opinion of the scholars regarding the prayer as being correct with a religious excuse and not correct without a sound religious excuse, said: 'This is the opinion of a group of scholars, and this is the view of Imaam Ahmad and others. It is the most sound and correct opinion, that not being ahead of the Imaam is one of the obligations of the prayer in congregation, and all obligations are waived under sound and valid excuses.' Therefore, since you prayed in front of the Imaam while you did not know that you were ahead of him, your prayer is permissible and you do not have to make it up. Your prayer outside the mosque is permissible since you heard the voice of the Imaam.
Allaah knows best.