ثم حبب إليه الخلاء وكان يخلو بغار حراء
“Thereafter, solitude was made dear to him. He would often go into seclusion in the cave of Hira.”
When Allah Ta‘ala intends to shower His special mercy upon a person, He instills the yearning for solitude and seclusion within this person’s heart.
Allah Ta‘ala mentions in the story of the people of the Kahf (cave):
وَإِذِ اعتَزَلتُموهُم وَما يَعبُدونَ إِلَّا اللَّـهَ فَأوا إِلَى الكَهفِ يَنشُر لَكُم رَبُّكُم مِن رَحمَتِهِ وَيُهَيِّئ لَكُم مِن أَمرِكُم مِرفَقًا ﴿١٦﴾
“And when you separate from them and from whatever else they worship besides Allah, then seek refuge in the cave so that your Lord may shower His mercy upon you and He may make your affairs easy for you.” [Surah Kahf v. 16]
This does not necessitate that a person will receive prophethood through solitude, because prophethood is not something that can be earned. Allah Ta‘ala appoints as messenger whomsoever He wishes.
(Extracted from Seeratul Mustafa 1/130-131)