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.
It is permissible for anyone who enters the Mosque to follow someone who has already begun an obligatory prayer, regardless of whether or not the Imaam (the one leading the prayer) had made the intention to lead others in prayer, according to the opinion of the majority of the scholars. Such a prayer will be counted as the obligatory prayer if one indented it as such.
It is permissible for one who performed prayer in congregation to perform prayer with someone who enters after the congregation prayer has already been performed. This second prayer for the former will be a supererogatory one. For more details, please refer to Fatwa 90854.
Allaah knows best.