Allah said, while warning against fleeing from the battlefield and threatening those who do it with the Fire,
﴿يَـأَيُّهَا الَّذِينَ ءَامَنُواْ إِذَا لَقِيتُمُ الَّذِينَ كَفَرُواْ زَحْفاً﴾
(O you who believe! When you meet those who disbelieve, in a battlefield,) when you get near the enemy and march towards them,
﴿فَلاَ تُوَلُّوهُمُ الأَدْبَارَ﴾
(never turn your backs to them.) do not run away from battle and leave your fellow Muslims behind,
﴿وَمَن يُوَلِّهِمْ يَوْمَئِذٍ دُبُرَهُ إِلاَّ مُتَحَرِّفاً لِّقِتَالٍ﴾
(And whoever turns his back to them on such a day -- unless it be a stratagem of war...)
The Ayah says, whoever flees from the enemy by way of planning to pretend that he is afraid of the enemy, so that they follow him and he takes the chance and returns to kill the enemy, then there is no sin on him. This is the explanation of Sa`id bin Jubayr and As-Suddi. Ad-Dahhak also commented, "Whoever went ahead of his fellow Muslims to investigate the strength of the enemy and make use of it,
﴿أَوْ مُتَحَيِّزاً إِلَى فِئَةٍ﴾
(or to retreat to a troop (of his own)), meaning he leaves from here to another troop of Muslims to assist them or be assisted by them. So that is allowed for him, or even during the battle if he flees from his brigade to the commander. Or going to the grand Imam, would also fall under this permission.''
`Umar bin Al-Khattab, may Allah be pleased with him, said about Abu `Ubayd when he was fighting on the bridge in the land of the Persians, because of the many Zoroastrian soldiers, "If he retreated to me then I would be as a troop for him.''
This is how it was reported by Muhammad bin Sirin from `Umar. In the report of Abu `Uthman An-Nahdi from `Umar, he said: When Abu `Ubayd was fighting, `Umar said, "O people! We are your troop.'' Mujahid said that `Umar said, "We are the troop of every Muslim.'' Abdul-Malik bin `Umayr reported from `Umar, "O people! Don't be confused over this Ayah, it was only about the day of Badr, and we are a troop for every Muslim.'' Ibn Abi Hatim ﴿recorded﴾ that Nafi` questioned Ibn `Umar, "We are people who are not stationary when fighting our enemy, and we may not know where our troop is, be it that of our Imam or our army.''
So he replied, "The troop is Allah's Messenger .'' I said but Allah said,
﴿إِذَا لَقِيتُمُ الَّذِينَ كَفَرُواْ زَحْفاًالنَّارِ﴾
(when you meet those who disbelieve in the battlefield) to the end of the Ayah . So he said; "This Ayah was about Badr, not before it nor after it.''
Ad-Dahhak commented that Allah's statement,
﴿أَوْ مُتَحَيِّزاً إِلَى فِئَةٍ﴾
(or to retreat to a troop), refers to "Those who retreat to the Messenger of Allah and his Companions (when the Messenger was alive), and those who retreat in the present time to his commander or companions.'' However, if one flees for any other reason than those mentioned here, then it is prohibited and considered a major sin. Al-Bukhari and Muslim recorded that Abu Hurayrah said that the Messenger of Allah said,
«اِجْتَنِبُوا السَّبْعَ الْمُوبِقَات»
("Shun the seven great destructive sins.'')
The people inquired, "O Allah's Messenger! What are they'' He said,
«الشِّرْكُ بِاللهِ وَالسِّحْرُ وَقَتْلُ النَّفْسِ الَّتِي حَرَّمَ اللهُ إِلَّا بِالْحَقِّ وَأَكْلُ الرِّبَا وَأَكْلُ مَالِ الْيَتِيمِ وَالتَّوَلِّي يَوْمَ الزَّحْفِ وَقَذْفُ الْمُحْصَنَاتِ الْغَافِلَاتِ الْمُؤْمِنَات»
((They are:) Joining others in worship with Allah, magic taking life which Allah has forbidden, except for a just cause (according to Islamic law), consuming Riba, consuming an orphan's wealth, fleeing the battlefield at the time of fighting, and false accusation to chaste women, who never even think of anything touching chastity and are good believers.)
This is why Allah said here,
(he indeed has drawn upon himself...), and returned with,
﴿بِغَضَبٍ مِّنَ اللَّهِ وَمَأْوَاهُ﴾
(wrath from Allah. And his abode...), destination, and dwelling place on the Day of Return,
﴿جَهَنَّمُ وَبِئْسَ الْمَصِيرُ﴾
(is Hell, and worst indeed is that destination!)
﴿فَلَمْ تَقْتُلُوهُمْ وَلَـكِنَّ اللَّهَ قَتَلَهُمْ وَمَا رَمَيْتَ إِذْ رَمَيْتَ وَلَـكِنَّ اللَّهَ رَمَى وَلِيُبْلِىَ الْمُؤْمِنِينَ مِنْهُ بَلاَءً حَسَنًا إِنَّ اللَّهَ سَمِيعٌ عَلِيمٌ - ذلِكُمْ وَأَنَّ اللَّهَ مُوهِنُ كَيْدِ الْكَـفِرِينَ ﴾
(17. You killed them not, but Allah killed them. And you threw not when you did throw, but Allah threw, that He might test the believers by a fair trial from Him. Verily, Allah is All-Hearer, All-Knower). (18. This (is the fact) and surely Allah weakens the deceitful plots of the disbelievers.)