The Treasury Department announced Tuesday that it is selling off the last of its shares in the insurance company AIG. That brings to a close one of the largest and most contentious components of the financial-rescue efforts, at a nearly $23 billion “profit” to taxpayers. At its peak, the taxpayers and the government were on the hook for $182 billion of investments and loans to the crippled insurer.

I’m not one to say I told you so. But in a March 2010 column I explained how AIG could wind up costing the taxpayers a measly few billion dollars. But even that view—wildly, insanely optimistic at the time—turned out to be too pessimistic. We actually made money on AIG.

How did this happen? AIG was the worst of the worst. It had sold huge amounts of credit insurance on subprime debt and other crappy instruments without setting aside adequate reserves. The failure of this unregulated shadow bank threatened to destroy the global financial system.

As it turned out, the ingredients for AIG’s success included a generous central bank and Treasury Department, buoyant Asian markets, a resilient U.S. economy, some solid core businesses, and an irascible CEO. So let’s consider each in turn.

Bailouts

In September 2008, the Federal Reserve extended an $85 billion credit line to the company, which paid down nearly half of that debt when the Treasury Department injected $40 billion of taxpayer funds into it. Next, the Fed effectively conducted a toxic-waste-removal operation, creating two investment vehicles to buy junky assets from AIG. The first, dubbed Maiden Lane II, borrowed $19.5 billion from the Fed and bought $20.8 billion in mortgage-backed securities at half their original price. The second, Maiden Lane III, borrowed $24.3 billion from the Fed and bought a portfolio of collateralized debt obligations (CDOs) from former AIG customers, also at about half their face value. In addition to providing funds, Treasury also offered AIG a line of credit.

The huge intervention was but a small subset of the trillions of dollars of guarantees, asset purchases, and interventions that the Fed and Treasury conducted in 2008 and 2009. Together, these easy-money policies helped stop the panic, created the conditions for growth, and reflated the credit markets.

Asian Markets

The continuing growth story in Asia also played a huge role in AIG’s comeback. Economic growth around the world suffered in late 2008 and early 2009, but Asia, and China in particular, powered through the slow times. AIG had historic and large operations in China and throughout the Far East. In March 2009, AIG turned over to the Fed two of its crown jewels, AIA (Asian insurance operations) and Alico (the U.S. life-insurance unit), in exchange for converting $25 billion of that credit into preferred shares in the two subsidiaries. AIG ultimately staged an initial public offering of AIA, a very solid business in a rapidly growing market, which raised about $20 billion. It would later sell Alico to Met Life. AIG generated enough cash from these sales—mostly from the sale of AIA—to pay off tens of billions in debt to the Fed. Put another way, other investors’ optimism about the future of AIG’s China operations provided a huge amount of cash.

U.S. Economy

At the same time, the U.S. economy recovered. One of the biggest underreported stories of the last couple of years has been the decline of failure. That is, consumers and companies are simply doing a much better job keeping up with financial obligations—credit cards, corporate loans, and especially mortgages. As a result, through 2009 and 2010, the mortgage bonds and related securities that the Fed had purchased from AIG at rock-bottom prices in 2008 gained value and threw off income. By selling assets and collecting interest payments, the Fed’s two vehicles, Maiden Lane II and Maiden Lane III, were able to generate enough cash to pay back the loans and then some. In February and June of this year, respectively, Maiden Lane II and Maiden Lane III retired their debt to the Federal Reserve—with interest and several billion dollars left over in profits.

The CEO

But there still remained all the money that Treasury had put into AIG that had converted into the stock. At its peak, Treasury owned about a 92 percent stake in the company, which included huge businesses in commercial, property, and life insurance in the U.S., Europe and elsewhere. And here’s where the CEO comes in. Robert Ben Mosche, the veteran insurance executive who came out of retirement to lead AIG even as he suffered from cancer, is hot-tempered and vain (read Jessica Pressler’s great New York magazine profile of him). But he has apparently done an excellent job keeping what was left of AIG’s empire together, generating profits, managing the relationship with its largest shareholder, and keeping the company’s stock at a sufficiently high level that it would allow Treasury to dispose of its position at a profit.

That last task was going to be the most difficult part, but here again it worked out quite well. And, of course, a few of the ingredients that had helped AIG in the early stages of its recovery came in handy. The Fed’s permissive money policies helped keep stocks afloat. And the continuing recovery in the U.S. made it a good place to have a large insurance business. In 2011 and 2012, as the stock market remained buoyant in the face of the debt-ceiling crisis, the European crisis, the election, and the fiscal cliff, Treasury was able to sell off its positions in AIG. It did so at first in small bites. But as the end neared, the share sales became larger. As Treasury pointed out in a press release, it has conducted six public offerings of AIG common stock in the past 19 months, selling shares at an average price of $31.18. It’s $20.7 billion offering in September “represented the largest single U.S. common stock offering in history.” That sale dramatically reduced Treasury’s stake to the small rump that was sold this week.

And so one of the least-wanted stock positions in American history has been unwound at a tidy profit.