In A.D. 627 the Muslims had to face the combined opposition of the Quraish, the Jews, and some other tribes. The Quraish and their allies mustered a force ten thousand strong and marched to Madina.
The Holy Prophet was advised that a face to face fight against such a large force was not possible for the Muslims, and the safest course for them was to remain on the defensive. It was accordingly decided that a deep and wide trench should be dug round Madina for protective purposes.
The entire Muslim community in Madina was turned to dig the trench. When the Muslims dug the trench the following war song was on their lips:
"By God, had not Allah guided us, we would not have seen the right path, nor given Sadaqah, nor offered the prayers;
May Allah bestow on us confidence and calmness of mind, and make our steps firm to face the enemy.
The enemy has risen against us, and he intends insurrection, but we refuse to submit.
O God there is no welfare except that of the next world;
Shower Thy Grace on the Ansars and the Muhajirs".
The site for the trench was demarcated by the Holy Prophet. He allotted ten yards of trench to be dug by each party of ten persons. One of such parties was led by Umar. Later a mosque known as Umar Masjid was constructed near the site where Umar and his party had dug the trench.
When the Quraish and their allies arrived they found that a wide and deep trench which could not be crossed lay between them and the Muslims. This was a mode of warfare with which the Quraish were not familiar. They camped beyond the trench and decided to besiege the Muslims.
The Holy Prophet divided the trench into a number of sectors and a contingent was posted to guard each sector. One of such contingents was under the command of Umar. One day the Quraish assaulted the sector commanded by Umar, and tried to carry the entrenchment by storm. Umar reinforced by Zubair beat back the enemy with a heavy hand and many Quraish were killed.
On another occasion Umar's contest with the Quraish was so prolonged that the time for the afternoon prayers passed away. After making the enemy beat a retreat, Umar came to the Holy Prophet, and told him how the enemy had kept him so hotly engaged that he had missed his afternoon prayers. The Holy Prophet said that he himself had not offered his afternoon prayers by that time. Then the Holy Prophet led the prayers and Umar and other companions offered their prayers.
The siege prolonged for a month, and the Muslims were subjected to great hardships. Food ran short, and subsistence I became a problem. The Muslims nevertheless persevered and hoped that God would come to their relief.
And God did come to the relief of the Muslims. There were dissensions in the camp of the enemy. Provisions with lifted ran short. Above all a strong storm lashed the country side. The Muslims were safe in their houses in Madina, but the Quraish who had to bear the brunt of the storm in the open were unnerved. Abu Sufiyan ordered that the sedge should be lifted, and the Quraish should withdraw to Mecca. Their allies also left.
The Muslims had the upper hand on account of their unusual perseverance against heavy odds and the inspired leadership of the Holy Prophet.