The Muslims in Mecca were suffering intense abuse at the hands of Quraysh, so the Prophet ﷺ first allowed them to emigrate to Abyssinia. Later, when many of the people of Medina had entered Islam, the migration was redirected toward Medina.
When the people of Mecca grew anxious over this turn of events, their notables and chiefs gathered in Dār al-Nadwah to devise a final solution to the Prophet ﷺ. They agreed to choose from every clan of Quraysh one courageous man who would strike him with their swords all at once. “That way,” they said, “his blood will be spread among the tribes, and Banū Hāshim will be unable to fight the whole of Quraysh and will accept blood-money.” They plotted, and Allah plotted—and Allah is the best of plotters.
Revelation then came to the Prophet ﷺ, so he resolved to migrate. He informed Abū Bakr of this and asked him to accompany him; Abū Bakr accepted. That very night, the night they had convened to assassinate him, he instructed ʿAlī to sleep in his bed, and he himself set out with Abū Bakr toward the cave. The assassins kept watch until daybreak. When dawn broke ʿAlī emerged, and they asked him, “Where is your companion?” He replied, “I do not know.”
They then searched for him in every direction, offering large rewards to whoever brought him back. The mountain that housed the cave filled with people seeking the Messenger of Allah ﷺ. Abū Bakr said, “O Messenger of Allah, if one of them were to look down at his own feet, he would see us.” He replied, “O Abū Bakr, what do you think of two with whom Allah is the third? (1)” Allah the Exalted then revealed:
“If you do not support him—Allah has already supported him—when those who disbelieved drove him out, the second of two, when the two of them were in the cave and he said to his companion, ‘Do not grieve; indeed, Allah is with us.’ Then Allah sent His tranquillity down upon him, strengthened him with forces you did not see, and made the word of those who disbelieved the lowest, while the word of Allah is supreme. Allah is Almighty, All-Wise.” (9:40)