The Treasury Department has sold its final stake in General Motors, closing the book on its 2009 bailout of the auto industry. GM has been revived and is now profitable, but taxpayers are out more than $10 billion dollars.

Treasury Secretary Jack Lew announced the final stock sale late Monday afternoon, saying that Treasury ultimately recouped $39 billion through the sale of shares, dividends and loan repayments since 2009. But the government pumped $49.5 billion into GM to help it get through a bankruptcy reorganization.

Treasury lost an estimated $1.3 billion in its bailout of Chrysler Group.

Still, the bailouts may have been a good deal for both the economy and the Treasury.

Lew said that if both companies had gone out of business, which was a serious risk in 2009, it would likely have caused widespread business failures among suppliers across the country. Experts said that it could have even forced other automakers such as Ford Motor (F) into bankruptcy, due to a lack of auto parts.

Lew said the government also would have been on the hook for pension payments for retired autoworkers that were backed by the Pension Benefit Guaranty Corp., a federal agency.

The failure of GM and Chrysler would have cost the federal government between $39 billion to $105 billion in lost tax revenues as well as assistance to the unemployed, according to a study released Monday by the Center for Automotive Research, a Michigan think tank.

"Our goal was never to make a profit but to stabilize the auto industry," said one Treasury official speaking to reporters on background Monday. "By any measure, we succeeded."

Related: We're almost break-even on the bailouts

The government initially had a 61% stake in GM when the company emerged from bankruptcy in 2009, leading critics of the bailout to dub it "Government Motors." It started to sell that stake with GM's November 2010 initial public offering.

The timing of the stock sale is in keeping with plans announced in January.

GM (GM) shares are up more than 40% so far this year. The company has earned nearly $20 billion in net income since 2010, the year it returned to profitability after emerging from bankruptcy.

GM says it has invested $8.8 billion into its U.S. plants since 2009, which resulted in the company either adding or maintaining 25,500 jobs at those facilities.

"We will always be grateful for the second chance extended to us and we are doing our best to make the most of it," said GM CEO Dan Akerson.

Akerson and other GM executives could be major beneficiaries of the end of the bailout. Until now the pay of GM executives was monitored by Kenneth Feinberg, the special master overseeing bailed out companies. Under Feinberg, Akerson got about $11.1 million in total compensation in 2012, which was about half the $21 million that Ford CEO Alan Mulally was paid.