General Motors Co posted its biggest quarterly profit in six years on Thursday and CEO Ed Whitacre stepped aside on the cusp of an IPO expected to allow the U.S. government to relinquish its majority stake.

Whitacre, who has served just eight months as chief executive of the top U.S. automaker, said he would step down on September 1, to be replaced by Dan Akerson, a GM board member and a managing director at private equity firm The Carlyle Group.

Whitacre's departure had been expected but the timing of the announcement was a surprise and came just a day ahead of GM's expected filing for a landmark stock offering.

Whitacre, who continued to commute from his home in Texas during his stint as CEO of the Detroit-based company, had said repeatedly that he would be an interim leader at GM.

It was obvious that I was not going to be at GM for the long haul, Whitacre said in a surprising announcement at the end of a conference call to discuss the company's second-quarter earnings. We have put a strong foundation in place, so I am very comfortable with my timing.

Akerson, a former CEO at Nextel, will become GM's fourth chief executive in just a year and a half, underscoring a continued challenge for an automaker analysts see as still in the early stages of a turnaround.

DRIVING THE TURNAROUND

Separately, GM posted a second-quarter profit of $1.3 billion in evidence of a turnaround driven by cost-cutting in its 2009 bankruptcy and better sales in the United States.

The second-quarter profit was the largest since 2004, when the U.S. auto market was still booming with annual sales of near 17 million vehicles and GM's brands accounted for more than one in four purchases of new cars and trucks.

The results reflected a 47 percent snap back in global production from the depressed levels of a year earlier when GM began operating under bankruptcy protection in a restructuring that included $50 billion in U.S. government funding.

Revenue rose to $33.2 billion from $31.5 billion in the first quarter, boosted by higher sales of more profitable new models such as the Chevrolet Equinox.

Analysts expect the company to use the results to build the case for a record stock offering and allow the U.S. government to reduce its 61 percent ownership stake.

A successful GM IPO would provide the Obama administration with evidence that the unprecedented and unpopular intervention in the U.S. auto industry has been a financial success.

Analysts said GM's results showed it can be profitable even in a still-weak U.S. auto market where sales are running at just over 11 million vehicles.

Europe, where GM is still struggling to restructure its Opel unit, remained a notable weak link for the automaker with an operating loss of $160 million.

North America had an operating profit of $1.6 billion. International operations, including GM's China joint ventures with SAIC and Wuling, had an operating profit of $672 million.

Despite GM's recovery over the past year, analysts say it still faces a challenge in winning back consumers because of the lingering stigma from its bailout and the Government Motors label from critics.

SNAPPING A LONG LOSING STREAK

GM lost about $88 billion between 2005 and 2009 when it was driven into bankruptcy by plunging sales and tight credit.

The last time the automaker had consecutive quarters of profits was in 2004, when it had a 26-percent share in a U.S. auto market that was near record-high levels.

GM's U.S. market share was just over 19 percent in the quarter that ended in June, down from almost 21 percent a year earlier when it was still selling the now-scrapped Saturn, Saab, Pontiac and Hummer brands.

Since GM remains privately held Wall Street equity analysts had not released earnings forecasts for it.

GM's results show it is trailing its more successful and smaller rival Ford Motor Co, which posted a second-quarter profit of $2.6 billion, but ahead of Chrysler, which lost $172 million.

The turnaround in GM's North American operations was most striking from a year ago. In the most recent quarter, GM ran its North American factories at 93 percent of capacity, compared with 39 percent a year earlier.

Sources told Reuters on Wednesday that the largest U.S. automaker had secured a $5 billion credit facility, marking its return to the capital markets a year after it emerged from a government-funded landmark bankruptcy.

GM Chief Financial Officer Chris Liddell declined to comment when asked about the credit facility. He said the arrangement was one of the building blocks GM needed to restore its balance sheet.

The $27 billion in bonds issued by the pre-bankruptcy GM represent a 10 percent equity stake in the new company under the terms of the restructuring negotiated by the White House.

GM's 8.375 percent bonds due in 2033 rose less than a penny on the dollar to 35.75 cents in trading on Thursday, yielding almost 24 percent.

The bond price has climbed from trading at 31 cents in early July, when the yield was more than 27 percent, according to MarketAxess data.

The higher bond price suggests that investors are placing a higher implied equity value on the automaker, which is expected to launch its stock offering by late November.

(Additional reporting by Bernie Woodall; Editing by Maureen Bavdek.)