(Hadith No. 1991)
Volume 3, Book 37, Number 495 :
Narrated by Abu Huraira (radiallaahu `anhu)
Whenever a dead man in debt was brought to Allah’s Apostle (sallallaahu `alayhi wasallam) 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 (sallallaahu `alayhi wasallam) 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.”
The first part of this hadith has been covered before. Read it here.
So initially the Prophet (sallallaahu `alayhi wasallam) would refuse to offer the funeral prayer of a person who hadn’t left anything to repay his debt(s) with.. but later on, when the Bayt-ul-Maal was sufficiently stocked, he would have their debts repaid from the state treasury, considering himself the guardian of the believers.
And that’s how it should be. If someone dies in debt leaving behind nothing, the state should repay their debts.