It’s taken almost five years, but one of the government’s ugliest ducklings is finally showing signs of turning into a swan. A profitable swan, at that. I’m talking about Ally Financial, formerly GMAC, General Motors’ finance subsidiary.

Ally sucked up $17.2 billion of federal bailout money, much of which ended up being used to fill the rathole known as Residential Capital (which we’ll call ResCap from here on). ResCap, once one of the nation’s largest mortgage companies, was Ally’s golden asset during the housing boom. Alas, it turned to dross when the housing bubble popped. Ally finally put it into bankruptcy last year.

It’s been a messy, contentious proceeding. But now it appears to be nearing an end, and Ally’s additional ResCap obligations seem to finally have a cap on them. That makes it possible to estimate what the government’s Ally investment is worth. Guess what? By my conservative math, that stake is worth $1.5 billion more than it cost. And I’m not including the $6.1 billion of dividends the government has collected from Ally. Taxpayers coming out ahead on a company that spent $8.3 billion, all of it lost, to keep ResCap afloat? That has committed another $2.3 billion to settle its ResCap liabilities? Yes, it’s true.

Given that there’s no public market for Ally shares, how can I calculate that the government is ahead? By digging through a bunch of financial filings and applying a little common sense. You can find the full version at

To give you the not-so-short version, I adjusted Ally’s stated net worth for the money it has set aside to pay ResCap’s creditors and regulators. Then I divided that net worth by the Ally shares outstanding. I took the per-share “book value” and multiplied it by the number of Ally shares the Treasury owns. I added the value of the Treasury’s Ally preferred stock and adjusted for a 2011 transaction in which the Treasury sold part of its Ally holdings. Bottom line: The stake is worth about $16 billion; the Treasury’s cost is about $14.5 billion.

Yes, I realize that even when you include the hefty dividend payments, you’re looking at a relatively modest profit for taxpayers, given the riskiness of the government’s investment. But the Treasury saved Ally from bankruptcy so that it could keep financing GM’s car dealers, allowing both the dealers and GM to avoid collapse. For taxpayers to come out ahead on this is an unexpected bonus.

How could it have possibly happened?

“Ally’s auto finance business has performed very well, and its international operations have been sold for excellent prices,” says Tim Massad, assistant Treasury secretary for financial stability, who adds that the company has substantially strengthened its Ally Bank subsidiary.

In addition, Ally’s liability in the ResCap bankruptcy was less than I originally expected, because Ally set up the bankruptcy well and ResCap’s assets were liquidated at excellent prices. Those prices are largely attributable to the fine job ResCap’s management did keeping the company from collapsing into chaos after it went into bankruptcy.

Timing, as they say, is everything. The Treasury bought into Ally during a panic, when assets were so cheap that the it could make out even though ResCap turned into a hideous nightmare. By contrast, Cerberus Capital and its private-equity investors are down almost 75 percent on their $8.15 billion Ally investment.

Cerberus spent $7.4 billion buying 51 percent of Ally from GM in 2006, when ResCap was rocking, and invested $750 million alongside the Treasury. Current value: $2.2 billion.

Now, a caveat or three. The ResCap bankruptcy settlement could unravel. Ally could run into unexpected problems. Its stock could fetch less than its asset value per share, the number I’m using. My bet, though, is that taxpayers will end up ahead on Ally. Isn’t it nice to get a pleasant surprise from the government for a change? 

Sloan is Fortune magazine’s senior editor at large.