It is complicated. You are correct in that when the house is sold investors get a recovery. Not all investors are created equal though. Difference pieces of a deal were paid different interest rates (according to risk) and as such they get allocated recoveries in a specific order. If you notice Toxie stopped getting payments a few months in and is declared dead a few months afterwards. This happens when the amount of losses eats through your piece of the pie. When the house gets sold is is sold for something less than the mortgage was worth. The losses get allocated to the riskier pieces of the deal which protects the lower interest rate/more "protected" higher pieces of the deal.
Also banks got bailouts which cushion them against losses but do not prevent losses. A loss on an asset is a loss on an asset. The federal reserve buying mortgage bonds was not considered a bailout per say but are helping to prop up prices which indirectly help the banks.