When Abu Talib was on his deathbed, the Messenger of Allah ﷺ came to him and found Abu Jahl and Abdullah bin Abu Umaya bin Mughira there with him.The Messenger of Allah ﷺ said, "Uncle, say, 'There is no god but Allah,' a word by which I may testify on your behalf before Allah."Abu Jahl and Abdullah bin Abu Umaya said, "Abu Talib, will you turn away from the religion of Abdul-Muttalib?"The Messenger of Allah ﷺ kept presenting it to him and repeating the same statement, until the last words Abu Talib spoke to them were that he was upon the religion of Abdul-Muttalib, and he refused to say, "There is no god but Allah."The Messenger of Allah ﷺ said, "By Allah, I will surely seek forgiveness for you so long as I am not forbidden from doing so."Then Allah revealed, "It is not for the Prophet and those who believe to ask forgiveness for the polytheists, even if they are close relatives, after it has become clear to them that they are companions of the Hellfire" [Tawbah 9:113].And Allah the Exalted revealed concerning Abu Talib, addressing the Messenger of Allah ﷺ, "Indeed, you do not guide whom you love, but Allah guides whom He wills, and He knows best those who are guided" [Qasas 28:56].
لما حضرت أبا طالب الوفاة جاءه رسول الله ﷺ، فوجد عنده أبا جهل، وعبد الله بن أبي أمية بن المغيرة، فقال رسول الله ﷺ: يا عم! قل: لا إله إلا الله. كلمة أشهد لك بها عند الله فقال أبو جهل وعبد الله بن أبى أمية: يا أبا طالب! أترغب عن ملة عبد المطلب؟ فلم يزل رسول الله ﷺ يعرضها عليه، ويعيد له تلك المقالة، حتى قال أبو طالب آخر ما كلمهم: هو على ملة عبد المطلب. وأبى أن يقول: لا إله إلا الله. فقال رسول الله ﷺ: أما والله لأستغفرن لك ما لم أنه عنك فأنزل الله عز وجل: {ما كان للنبي والذين ءامنوا أن يستغفروا للمشركين ولو كانوا أولي قربى من بعد ما تبين لهم أنهم أصحب الجحيم} وأنزل الله تعالى في أبي طالب، فقال الرسول الله ﷺ: {إنك لا تهدي من أحببت ولكن الله يهدى من يشاء وهو أعلم بالمهتدين} [القصص: ٥٦].
Bukhari related it in the tafsir (4675), and Muslim in the faith (24), both of them through Ibn Shihab, who said, "Saeed bin Musayab informed me, from his father," and he related it. The wording is Muslim's, and Bukhari's wording is abridged.