Whenever a believer died owing a debt, the Messenger of Allah ﷺ would ask, "Did he leave behind anything to settle his debt?"If they said yes, he would pray over him. If they said no, he would say, "Pray over your companion yourselves."When Allah granted His Messenger ﷺ the conquests, he said, "I am closer to the believers than they are to themselves. Whoever dies leaving a debt, its repayment is upon me, and whoever leaves wealth, it belongs to his heirs."
أخبرنا يونس بن عبد الأعلى قال أنبأنا ابن وهب قال أخبرني يونس وابن أبي ذئب عن ابن شهاب عن أبي سلمة
أن رسول الله ﷺ كان إذا توفي المؤمن وعليه دين سأل هل ترك لدينه من قضاء فإن قالوا نعم صلى عليه وإن قالوا لا قال صلوا على صاحبكم فلما فتح الله عز وجل على رسوله ﷺ قال أنا أولى بالمؤمنين من أنفسهم فمن توفي وعليه دين فعلي قضاؤه ومن ترك مالا فهو لورثته
Muslim narrated it in the Book of Inheritance, chapter "Whoever leaves behind wealth, it goes to his heirs," hadith no. 1619, through Abdullah bin Wahb, and Bukhari narrated it in the Book of Inheritance, chapter "The saying of the Prophet, 'Whoever leaves behind wealth, it goes to his family,'" hadith no. 6731, through Yunus bin Yazid, both in shortened and full versions. It is also found in Kubra, hadith no. 2090.