Abu Hurayrah reported that the Messenger of Allah ﷺ would have a deceased man brought to him for the funeral prayer, and he would ask, "Did he leave behind enough to cover his debt?" If he was told that the man had left enough to settle his debt, he would pray over him. Otherwise, he would say to the Muslims, "Pray over your companion."When Allah granted him the conquests, he said, "I am closer to the believers than they are to themselves. Whoever among the believers dies leaving a debt, it is upon me to pay it, and whoever leaves wealth, it belongs to his heirs."
أن رسول الله ﷺ كان يؤتى بالرجل المتوفى عليه الدين، فيسأل: هل ترك لدينه فضلا؟ فإن حدث أنه ترك لدينه وفاء صلى، وإلا قال للمسلمين: صلوا على صاحبكم. فلما فتح الله عليه الفتوح قال: أنا أولى بالمؤمنين من أنفسهم، فمن توفي من المؤمنين فترك دينا فعلي قضاؤه، ومن ترك مالا فلورثته.
Bukhari narrated it in the Book of Suretyship (2298), and Muslim in the Book of Inheritance Shares (1619), both of them through Ibn Shihab Zuhri, from Abu Salama bin Abdul-Rahman, from Abu Hurairah, who mentioned it. The wording given is Bukhari's, and Muslim's wording is similar.