Hazrat Urwah bin Zubair (may Allah have mercy on him) narrates from Mardan bin Hakam that he asked Sayyiduna Abu Hurairah (may Allah be pleased with him), "Did you offer the prayer of fear (Salat al-Khawf) with the Noble Prophet (peace and blessings be upon him)?" Sayyiduna Abu Hurairah (may Allah be pleased with him) replied, "Yes." He asked, "When?" He replied, "In the year of the Battle of Najd." The Messenger of Allah (peace and blessings be upon him) stood for the Asr prayer and one group stood with him, while the other group stood facing the enemy, their backs towards the Qiblah. The Messenger of Allah (peace and blessings be upon him) said « اللهُأَكْبَرُ », so all the people said « اللهُأَكْبَرُ » and began the prayer, both those who stood with him and those who were facing the enemy. Then the Messenger of Allah (peace and blessings be upon him) prayed one rak'ah and the group with him also performed one rak'ah. Then the Messenger of Allah (peace and blessings be upon him) and his group performed the prostrations, while the other group remained arrayed before the enemy. Then the Messenger of Allah (peace and blessings be upon him) and his close row stood up. So they went to face the enemy. And the group that was arrayed before the enemy came forward, so they performed the bowing (ruku') and (two) prostrations, while during this time the Messenger of Allah (peace and blessings be upon him) remained standing. Then they stood up, so the Messenger of Allah (peace and blessings be upon him) prayed the second rak'ah, so they also performed the bowing and prostrations with him. Then the group that was standing before the enemy came forward, so they (by themselves) performed the second bowing and prostrations, while during this time the Messenger of Allah (peace and blessings be upon him) and his companions (the first group) remained seated. Then the Messenger of Allah (peace and blessings be upon him) said the salam, and the Messenger of Allah (peace and blessings be upon him) and all the people said the salam together. In this way, the Messenger of Allah (peace and blessings be upon him) completed two rak'ahs, and each person from both groups also completed two rak'ahs.
If the enemy is behind the qiblah and the imam waits standing after finishing the first rak‘ah for the group that made takbir not facing the qiblah, then they pray the rak‘ah which the imam preceded them with, and the first group waits sitting after finishing two rak‘ahs before the salam, so that the second rak‘ah may be completed, in order to unite them all with the salam, and they give salam when their imam gives salam.
Hadith Referenceصحيح ابن خزيمه / جماع أبواب صلاة الخوف / 1361