The Federal Housing Finance Agency, which regulates Fannie Mae and Freddie Mac, has been waging a battle to recoup billions of dollars in mortgage-securities losses suffered by the lending giants. And the fight is starting to pay off.

In the past year, three of the 18 financial firms — including General Electric and Citigroup — that were sued by the regulator for knowingly selling Fannie and Freddie securities packaged with bad home loans have settled. Now JPMorganChase is close to striking a $13 billion deal, with at least $4 billion earmarked to resolve its case with the housing agency.

But where will all that money go?

Since 2008, after a $188 billion taxpayer bailout, Fannie Mae and Freddie Mac have been the wards of the government. Part of the reason behind the massive rescue was the losses sustained by the firms, which bought more than $200 billion in mortgage securities that soured once the housing market crashed.

Although it seems that the money recouped from those losses would be used to pay down the hefty tab the mortgage-finance twins accumulated, that’s not exactly how it works.

When the Treasury Department threw the drowning Fannie Mae and Freddie Mac life preservers in 2008, the agency took “preferred” stock in exchange, which requires the lending firms to pay a dividend. But Fannie Mae and Freddie Mac are not allowed to repay the principal amount provided to them.

The dividends paid to Treasury, roughly $140 billion, go into coffers to finance the operation of the federal government.

Up until a year ago, Treasury collected 10 percent dividend payments, but it amended the terms of the agreement to also gain most of the lending firms’ profits. This structure has created an acrimonious relationship between the agency and Fannie Mae and Freddie Mac shareholders, who are suing to strike down the amendment and gain a greater share of the dividends.

The mortgage-finance firms have rebounded to tremendous profitability in the past two years as the housing market has recovered. In its most recent earnings report, Fannie Mae posted a $10.1 billion profit for the second quarter and sent a $10.2 billion dividend payment to Treasury. Freddie Mac reported net income of $5 billion and made a $4.4 billion payment to the government.

Whatever money the finance firms have coming from the FHFA lawsuits will go the way of the dividends — straight to Treasury. In 2011, the housing agency filed more than a dozen lawsuits at once against financial companies for making false representations about the quality of the bundled mortgages. The FHFA claims that the rate of owner occupancy was overstated, and the average loan-to-value in the mortgage pool was understated.

Attorneys for many of the firms have argued that the banks that originated the home loans, not the firms that sold the mortgage bonds, are liable for false statements. Companies tried to have the cases thrown out on the basis that the FHFA waited too long to sue them, but the courts have rejected that argument.

Analysts say it was that court decision that probably led firms such as UBS, which agreed to a $885 million settlement in July, to strike a deal with the housing agency. Many predict that other firms will follow in the coming months.