The Battle of the Trench was a two-day battle fought in January 627 between the armies of Islam under Muhammad and the Quraysh under Abu Sufyan. The outnumbered 3,000-strong Muslim army built a trench that circled the city of Medina at Salman the Persian's advice, as it would prevent the Quraysh from using cavalry in the battle. The Muslims successfully defended the city from the larger 10,000-strong Quraysh army, another major victory for the Muslims.
The 625 Battle of Uhud was a disaster for the Muslims, costing them several of their bravest warriors and crushing their morale - it was feared that Muhammad had been killed by a rock in the battle. However, Muhammad's survival led to his men rejoicing, and it was the pagan Quraysh leader Abu Sufyan who had nothing to celebrate. He decided to band together with the other Arab tribal chiefs to form an alliance against Muhammad, and he gathered an army of 10,000 men to besiege Muhammad's stronghold of Medina. Muhammad, who had 3,000 men at his command, decided to build a trench around the city to protect it from cavalry, as Salman the Persian explained that the victorious Sassanids had often used the strategy. This would prevent a disaster such as Uhud, and it would take away the Quraysh's advantage.
Abu Sufyan dispatched Khalid ibn al-Walid to lead cavalry to the narrowest section of the gap and attempt to hop across the gaps, but many horses and men fell to their deaths in failed jumps. Those who struggled were shot at by Muslim archers, causing Abu Sufyan to reluctantly call off the attack. However, he ordered his generals to prepare for an all-out assault at dawn. Both Abu Sufyan and Muhammad prayed before the second attack, and the large Quraysh army arrived at dawn. The two sides faced off, but no fighting took place; Muhammad used diplomacy to stop some of the Arab chiefs from attacking, and a sandstorm caused the torches in the Quraysh camp to blow over onto the tents, setting the camp on fire. The blinded Quraysh troops had no choice but to retreat to Mecca, and the "battle" was a Muslim victory.
Muhammad then had a dream in which he held the keys of the Ka'aba and entered it, and he announced that he was going to lead his unarmed companions into the city on a pilgrimage. Abu Sufyan knew that all of Arabia would rise up against the Quraysh if he was to order the massacre of unarmed pilgrims, and he was forced to bear the shame of offering Muhammad a peace treaty. The two sides would stop fighting, and there would be a ten-year peace, with Muslims being allowed to enter the city the next year. Eventually, the Quraysh generals Khalid ibn al-Walid and 'Amr ibn al-'As decided to enter the city and convert to Islam upon Muhammad's entrance in 630, and the wars came peacefully to an end as even Abu Sufyan converted.