He had vowed during the days of ignorance to spend a night in spiritual retreat, so he asked the Messenger of Allah ﷺ about it, and he instructed him to fulfill the retreat.
أخبرنا إسحق بن موسى قال حدثنا سفيان عن أيوب عن نافع عن ابن عمر
أنه كان عليه ليلة نذر في الجاهلية يعتكفها فسأل رسول الله ﷺ فأمره أن يعتكف
Bukhari, itikaf, chapter on those who do not consider fasting obligatory when observing itikaf, no. 2042, and Muslim, oaths, chapter on the vow of the disbeliever and what should be done about it when he becomes Muslim, no. 1656, from the hadith of Nafi through him, and it is in Kubra, no. 4762.