مَا كَانَ لِأَهْلِ ٱلْمَدِينَةِ وَمَنْ حَوْلَهُم مِّنَ ٱلْأَعْرَابِ أَن يَتَخَلَّفُوا۟ عَن رَّسُولِ ٱللَّهِ وَلَا يَرْغَبُوا۟ بِأَنفُسِهِمْ عَن نَّفْسِهِۦ ۚ ذَٰلِكَ بِأَنَّهُمْ لَا يُصِيبُهُمْ ظَمَأٌ وَلَا نَصَبٌ وَلَا مَخْمَصَةٌ فِى سَبِيلِ ٱللَّهِ وَلَا يَطَـُٔونَ مَوْطِئًا يَغِيظُ ٱلْكُفَّارَ وَلَا يَنَالُونَ مِنْ عَدُوٍّ نَّيْلًا إِلَّا كُتِبَ لَهُم بِهِۦ عَمَلٌ صَـٰلِحٌ ۚ إِنَّ ٱللَّهَ لَا يُضِيعُ أَجْرَ ٱلْمُحْسِنِينَ ١٢٠
It was not [proper] for the people of Madīnah and those surrounding them of the bedouins that they remain behind after [the departure of] the Messenger of Allāh or that they prefer themselves over his self. That is because they are not afflicted by thirst or fatigue or hunger in the cause of Allāh, nor do they tread on any ground that enrages the disbelievers, nor do they inflict upon an enemy any infliction but that it is registered for them as a righteous deed. Indeed, Allāh does not allow to be lost the reward of the doers of good.
Tafsir Ibn Kathir (abridged)
Ismail ibn Kathir
Rewards of Jihad
Allah, the Exalted and Most Honored, criticizes the people of Al-Madinah and the bedouins around it, who did not participate in the battle of Tabuk with the Messenger of Allah ﷺ. They sought to preserve themselves rather than comfort the Messenger ﷺ during the hardship that he suffered in that battle. They incurred a loss in their share of the reward, since,
لاَ يُصِيبُهُمْ ظَمَأٌ
(they suffer neither Zama'), thirst,
وَلاَ نَصَبٌ
(nor Nasab), fatigue,
وَلاَ مَخْمَصَةٌ
(nor Makhmasah), hunger,
وَلاَ يَطَأُونَ مَوْطِئًا يَغِيظُ الْكُفَّارَ
(nor they take any step to raise the anger of disbelievers), by strategies of war that would terrify their enemy,
وَلاَ يَنَالُونَ
(nor inflict), a defeat on the enemy,
إِلاَّ كُتِبَ لَهُمْ
(but is written to their credit) as compensation for these steps that are not under their control, but a consequence of performing good deeds that earn them tremendous rewards,
إِنَّ اللَّهَ لاَ يُضِيعُ أَجْرَ الْمُحْسِنِينَ
(Surely, Allah wastes not the reward of the doers of good.) Allah said in a similar Ayah,
إِنَّا لاَ نُضِيعُ أَجْرَ مَنْ أَحْسَنَ عَمَلاً
(Certainly We shall not make the reward of anyone who does his (righteous) deeds in the most perfect manner to be lost)
Commentary
The two verses cited above admonish those who missed to join the expedition of Tabuk for no valid reason. They have also been told that they did what was not proper. Then the verses describe the merits of people who take part in Jihad and tell us about the great rewards they get for everything they do there. Included here is the causing of hurt or loss to the enemy and the using of a strategy that infuriates the adversary. All these are good deeds, and worthy of thawab (reward from Allah).
A third group consisted of those who were left behind. These were three youths, Ka‘b ibn Malik, Mararah ibn Rubay and Hilal ibn Umayyah. Though they regarded their inability to proceed on this journey as a shortcoming, their feelings of repentance and turning towards God had not been as intense as was required in this first stage. That is why they were punished by a social boycott. Now even with this boycott these three had a number of options. They could have remained happy and contented; they could have remained engrossed in their homes and gardens; they could have become rebellious; they could have formed their own separate group in collaboration with disgruntled elements, thus forming an island, a happy world cut off from the general body of Muslims. But they refrained from doing so. The thought of having earned the displeasure of God and His Prophet disturbed their minds so greatly that they failed to find a moment’s peace. For them this was an option, not a compulsion. For they could have escaped by rebelling against this boycott and joining any other group. But their response being positive, they naturally turned to God for forgiveness. They repented and prayed to God. Their hearts were truly softened. After a period of 50 days of prayer God showed them His mercy and they were pardoned.
Uthmani text of the Madinah mushaf (King Fahd Complex). Translation: Saheeh International. Tafsir via Quran.com.