Treasury says it will unload 500 million shares in the automaker that it held under the industry bailout. (David Zalubowski/AP)

The much-debated bailout of Detroit is finally nearing an end after four years — and it looks like the ultimate cost to taxpayers will be between $10 billion and $20 billion.

Having already disposed of its stake in Chrysler, the Treasury Department announced Wednesday that it is now planning to sell its 500 million shares of General Motors stock. The company is buying back 200 million of those shares for $5.5 billion, while the Treasury is developing a plan to sell the rest over the next 12 to 15 months.

If the remaining stock sells at $27.18 a share, GM’s price as of 4 p.m. Wednesday, the government will have sustained a final loss of about $12 billion in its investment in GM. Treasury lost $1.3 billion on Chrysler and still is owed $11.4 billion by Ally Financial, formerly GM’s financing affiliate, to come out even on that investment.

Though the auto bailout is likely to produce a financial loss for the federal government, it was part of a financial rescue that far exceeded expectations. Treasury has recouped 90 percent of the $418 billion it invested in banks, autos and other firms as part of the overall financial bailout.

“The auto industry rescue helped save more than a million jobs during a severe economic crisis, but [the intervention] was always meant to be a temporary, emergency program,” said Timothy G. Massad, Treasury assistant secretary for financial stability, in a statement. “The government should not be in the business of owning stakes in private companies for an indefinite period of time.”

The Treasury’s original total investment in the auto industry was about $80 billion, including $50 billion in GM.

The government’s exit from GM will ultimately free the firm of constraints that executives have said limited how they could run the business. For example, executive compensation caps made it difficult to recruit top business leaders, and the company could not own corporate jets. The compensation restrictions remain for now, but the jet provision has been lifted.

Executives also worried about having a reputation as a federally restricted “Government Motors.”

“This announcement is an important step in bringing closure to the successful auto industry rescue, it further removes the perception of government ownership of GM among customers, and it demonstrates confidence in GM’s progress and our future,” said Dan Akerson, GM’s chief executive, in a statement.

The bailout of Detroit was initiated under President George W. Bush in late 2008 and expanded by President Obama. GM and Chrysler were on the verge of imminent bankruptcy — weighed down by overwhelming pension costs, a deep decline in sales and dwindling cash reserves.

Some objected to the bailout because it involved the government deeply in the private market. But many economists agree that restructuring the automobile industry was critical to the government’s efforts to pull the economy out of a deep recession.

The bailout was as an issue in the presidential campaign, with the Republican nominee, Mitt Romney, deeming it inappropriate for the government to hold shares in the industry.

But at the Democratic National Convention, the Obama campaign celebrated the rebirth of the American auto industry, with Vice President Biden saying, “Bin Laden is dead and General Motors is alive.”

Tens of thousands of jobs at car dealerships may have been lost as a result of government directives that General Motors and Chrysler shut them to save costs.

Since stabilizing, the auto companies have enjoyed a robust rebound. The companies have added 260,000 jobs since the recession ended, though they are still far below their employment peaks.