Answered by Shaykh Abul ‘Ataa Ahmad Banaja (may Allah preserve him)
When a Muslim forgets to pray (for eg: Duhr prayer) out of sleep or because he got busy in work. And when he went to masjid Imam was praying the ‘Asr prayer. So, is this person supposed to pray ‘Asr with the Imam?
If you come to the Masjid and the people are already praying ‘Asr in congregation, but you haven’t prayed Duhr, without doubt you have to join the congregation with the intention of Duhr. After its finished, you have to pray ‘Asr.
This is because following the order in prayers is obligatory.
This came clearly in the narration of Jabir in Bukhari:
أنَّ عُمَرَ بنَ الخَطَّابِ، جَاءَ يَومَ الخَنْدَقِ، بَعْدَ ما غَرَبَتِ الشَّمْسُ فَجَعَلَ يَسُبُّ كُفَّارَ قُرَيْشٍ، قَالَ: يا رَسولَ اللَّهِ ما كِدْتُ أُصَلِّي العَصْرَ، حتَّى كَادَتِ الشَّمْسُ تَغْرُبُ، قَالَ النبيُّ صَلَّى اللهُ عليه وسلَّمَ: واللَّهِ ما صَلَّيْتُهَا فَقُمْنَا إلى بُطْحَانَ، فَتَوَضَّأَ لِلصَّلَاةِ وتَوَضَّأْنَا لَهَا، فَصَلَّى العَصْرَ بَعْدَ ما غَرَبَتِ الشَّمْسُ، ثُمَّ صَلَّى بَعْدَهَا المَغْرِبَ
On the day of Al-Khandaq (the battle of trench.) Umar bin Al-Khattab came cursing the disbelievers of Quraish after the sun had set and said, “O Allah’s Messenger (ﷺ) I could not offer the Asr prayer till the sun had set.” The Prophet (ﷺ) said, “By Allah! I, too, have not prayed.” So we turned towards Buthan, and the Prophet (ﷺ) performed ablution and we too performed ablution and offered the Asr prayer after the sun had set, and then he offered the Maghrib prayer.
The same ruling would apply, if you attended the congregation while they were praying ‘Ishaa and you haven’t prayed Maghrib. You join the congregation with the intention of Maghrib with them. Then you give Tasleem, join with them if you could, for ‘Ishaa.
The same ruling is if, you missed one prayer and the time of second prayer entered and its time is about to end. For example, if you missed Duhr and you also haven’t prayed ‘Asr, and remembered it few minutes before Maghrib, and you have enough time to pray ‘Asr only, you must pray in order. That is, you pray Duhr first, then ‘Asr, even if the time of Maghrib entered. After that you have to pray Maghrib. According to the correct opinion.
In summary, following the order in prayers is obligatory and you must not break the order in any case.