As millions of Americans struggle to hold on to their homes, Wall Street has found a way to make money from the mortgage mess.

Investment funds are buying billions of dollars’ worth of home loans, discounted from the loans’ original value. Then, in what might seem an act of charity, the funds are helping homeowners by reducing the size of the loans.

But as part of these deals, the mortgages are being refinanced through lenders that work with government agencies like the Federal Housing Administration. This enables the funds to pocket sizable profits by reselling new, government-insured loans to other federal agencies, which then bundle the mortgages into securities for sale to investors.

While homeowners save money, the arrangement shifts nearly all the risk for the loans to the federal government  and, ultimately, taxpayers  at a time when Americans are falling behind on their mortgage payments in record numbers.