All perfect praise be to Allah the Lord of the Worlds. May His peace and blessings be upon our Prophet Mohammad and upon all his family and companions.
Contracts are based on their apparent terms because this is more conducive to the stability of financial transactions between people. The effects of the contract pertain to the one who directly engages in it. Shaykh al-Islam Imam al-Nawawi, may Allah have mercy on him, said: "Consideration is given to the one who makes the contract because the rulings of the contract pertain to him" [Al-Majmu' Sharh al-Muhadhdhab. Vol. 9/P. 386]. The one who makes the contract is the one who undertakes the contract for himself or for another by his (the latter`s) authorization. If such authorization does not exist, the contract pertains to the one who made it himself.
If the matter is as mentioned in the question, the true ownership of the apartment belongs to the person whose name the transaction was under (the deceased), and the ownership is his. He is the beneficiary of the bank's cancellation of the remaining amount because death is the reason for the cancellation. His (deceased`s) sale of the apartment to his relative is a separate sale and a different contract from the first one, and it has no relation to it. The first person bought the apartment from the bank, then sold it with a profit to another. The cancellation of the amount and the waiver pertain to the holder of the first contract (the deceased), and it has no relation to the holder of the second contract. Therefore, the second buyer - who took the apartment from the deceased - continues to pay the remaining installments to the heirs.
In "Al-Bayan Fi Madhhab Al-Imam Al-Shafi'i, Vol.5/P.335" it is stated: "If the first buyer sold it by verbally using the term 'I sold it to you,' and then the price was waived for the first buyer, the second buyer is not affected by the price waiver."
In conclusion, the waiver of the remaining price benefits the heirs because the reason for the waiver is the death of the person whom they inherited. In addition, the second sale is not linked to the first sale, as each contract has its own effects and is distinct from the other contract. And Allah the Almighty knows best.