Whenever a dead man in debt was brought to Messenger of Allah ﷺ he would ask, "Has he left anything to repay his debt?" If he was informed that he had left something to repay his debts, he would offer his funeral prayer, otherwise he would tell the Muslims to offer their friend's funeral prayer. When Allah made the Prophet ﷺ wealthy through conquests, he said, "I am more rightful than other believers to be the guardian of the believers, so if a Muslim dies while in debt, I am responsible for the repayment of his debt, and whoever leaves wealth (after his death) it will belong to his heirs. " (Using translation from Bukhārī 2298)
أَنَّ رَسُولَ اللهِ ﷺ كَانَ يُؤْتَى بِالرَّجُلِ الْمُتَوَفَّى عَلَيْهِ الدَّيْنُ فَيَسْأَلُ هَلْ تَرَكَ لِدَيْنِهِ مِنْ قَضَاءٍ؟ فَإِنْ حُدِّثَ أَنَّهُ تَرَكَ وَفَاءً صَلَّى عَلَيْهِ وَإِلَّا قَالَ لِلْمُسْلِمِينَ صَلُّوا عَلَى صَاحِبِكُمْ فَلَمَّا فَتَحَ اللهُ عَلَيْهِ الْفُتُوحَ قَامَ فَقَالَ أَنَا أَوْلَى بِالْمُؤْمِنِينَ مِنْ أَنْفُسِهِمْ فَمَنْ تُوُفِّيَ مِنَ الْمُسْلِمِينَ فَتَرَكَ دَيْنًا فَعَلَيَّ قَضَاؤُهُ وَمَنْ تَرَكَ مَالًا فَهُوَ لِوَرَثَتِهِ