I don't know enough to make a moral judgement on any of this. But we don't have all the facts. I'm sure some accountant somewhere worked it out so it made sense.
Someone worked out that the house would get more than the debt that was bought from the bank. So they might have made money on it and the bank wrote off the difference.