Of all the Wall Street bailouts amid the Panic of 2008, none was more despised than the $182 billion lifeline for American International Group.

A global insurer hungry for profits, AIG staked its reputation for financial strength on a risky derivatives business that Federal Reserve Chairman Ben S. Bernanke likened in 2009 to a gigantic hedge fund. The firm sold credit protection to the buyers of shaky mortgage-backed securities, and when those securities tanked, so did AIG.

As of Sept. 16, 2008, when the Federal Reserve stepped in with an $85 billion line of credit, the company had recklessly engaged so many counterparties, including European banks and Wall Street giants such as Goldman Sachs, that its failure could have been even more devastating to the world economy than the collapse of Lehman Brothers, which had occurred only a day earlier.

Eventually, the Bush and Obama Treasury departments used money from the Troubled Asset Relief Program (TARP) to take equity in AIG; as of March 2009, the government owned 92 percent of the company.

AIG’s nationalization was widely condemned as an egregious case of the “too big to fail” syndrome. It was also widely expected to be a disaster, in the sense that the company could never be restructured without huge taxpayer losses.

Well, don’t look now, but the AIG bailout is working. On Friday, the Treasury announced that it was selling $5 billion worth of AIG stock, its fourth such sale, bringing its stake in the company down to 55 percent. Treasury made $300 million on the transaction, consistent with the fact that markets value the stock above the government’s $28.72 break-even share price. Contrast bailed-out GM, where Treasury will cash out at a loss in almost any realistic stock-price scenario.

Treasury’s remaining $25 billion equity stake in AIG represents only 14 percent of the original federal commitment (of which AIG used about $140 billion). All the rest has been paid back, including AIG’s debt to the Federal Reserve, which made $13 billion on the deal.

Buoyed by bullish analyst reports, AIG could be majority-owned by the private sector in months — and Treasury may be able to get completely out of its stake far sooner than anyone predicted three years ago.

What went right? Credit goes to the new AIG management under chief executive Robert Benmosche, which has raised billions by selling assets — and unwound the vast majority of the erstwhile derivatives business. That portfolio, about $2 trillion in 2008, was down to $168 billion as of the first quarter of this year.

The Obama administration Treasury Department deserves praise, too, for keeping tabs on Benmosche while generally letting him do his thing and for prudently managing the public’s stake in the firm.

Of course, difficult issues remain. The special inspector general for TARP published a report last month pointing out that AIG remains far-flung and complex, with more than 200 subsidiaries in 130 countries. The report noted that “there is currently no Federal banking regulator with responsibility for overseeing AIG’s non-insurance businesses.” Spencer Bachus, the Republican chairman of the House Financial Services Committee, echoed those points at a hearing, adding that Treasury must “map out an exit strategy.”

It’s true that no government regulator is assigned to AIG at the moment. But why is that such a disaster for a firm that’s still owned by the government?

And once Treasury does give up its majority stake, it will probably pass the regulatory baton to the Fed, which would oversee AIG as a “systemically important financial institution” under the Dodd-Frank law.

The inspector general worries that oversight of AIG may be too complex for the Fed. But given AIG’s history, it seems likely that the Fed will err on the side of caution. That’s especially true now that Dodd-Frank has ended the Fed’s power to extend crisis loans to individual firms.

As for mapping an “exit strategy,” it would appear that Treasury is quietly pursuing one, as opposed to the money-losing approach of announcing its market moves in advance.

The AIG bailout epitomized “moral hazard.” As such, nothing can truly redeem it. Even if taxpayers come out ahead on the transaction, that would mitigate but not necessarily offset the bad precedent that has been set.

What can and does justify the bailout is the near-certainty that the alternatives would have been much worse. We can calmly debate the AIG rescue today because we are not coping with a second Great Depression.

So, Ben Bernanke, Tim Geithner and Robert Benmosche — congratulations! Just don’t let it happen again.