The Messenger of Allah ﷺ used to be brought the body of a man who had died in debt, and he would ask, "Did he leave anything to settle his debt?"If he was told that the deceased had left enough to cover it, he would pray over him. Otherwise, he would say to the Muslims, "Pray over your companion."Then, when Allah granted him the conquests, he stood up and said, "I am closer to the believers than they are to themselves. Whoever among the Muslims dies leaving a debt, its payment is upon me, and whoever leaves wealth, it belongs to his heirs."
حدثني أبو الفضل مكتوم بن العباس الترمذي حدثنا عبد الله بن صالح قال حدثني الليث قال حدثني عقيل عن ابن شهاب قال أخبرني أبو سلمة بن عبد الرحمن
أن رسول الله ﷺ كان يؤتى بالرجل المتوفى عليه الدين فيقول هل ترك لدينه من قضاء فإن حدث أنه ترك وفاء صلى عليه وإلا قال للمسلمين صلوا على صاحبكم فلما فتح الله عليه الفتوح قام فقال أنا أولى بالمؤمنين من أنفسهم فمن توفي من المسلمين فترك دينا علي قضاؤه ومن ترك مالا فهو لورثته
Reference, agreed upon by Bukhari and Muslim. Bukhari reported it in the book of Expenditures, chapter "The saying of the Prophet ﷺ, 'Whoever leaves behind a burden or dependents, it falls upon me,'" hadith no. 5371, and Muslim, hadith no. 1619, both from the hadith of Layth bin Saad, with the same chain.