By Paul Lienert

DETROIT (Reuters) - Electric-car maker Tesla Motors Inc surprised Wall Street on Wednesday, reporting a second-quarter profit of 20 cents a share versus an expected non-GAAP loss of 17 cents.

Investors pushed the stock as high as $153 in after-hours trading, up 14 percent from its Wednesday close at $134.

Tesla raised more than $1 billion in equity and debt in May and paid off a $440-million government loan balance. It closed the quarter with $746 million in cash and said it expects its gross margin to climb to 22 percent by year-end.

As Tesla has ramped up production of its Model S sedan -- the best-selling U.S. electric car despite a starting price of more than $70,000 -- the company's stock price has more than quadrupled in the past year.

Tesla said it sold 10,500 Model S sedans in the first six months and expects to reach an annualized rate of 40,000 by late 2014.

Revenues in the quarter were $405 million, down from $562 million in the first quarter, reflecting an accounting requirement that defers some revenue on vehicles financed through Tesla and its financial partners. On a non-GAAP (Generally Accepted Accounting Principles) basis, revenues rose to $551 million.

The company said it lost $30.5 million. On a non-GAAP basis, however, it reported net income of $26 million, excluding lease accounting, non-cash items and one-time charges associated with its early payoff of a U.S. Department of Energy loan.

Analysts, on average, expected Tesla to post a loss of 17 cents per share, according to Thomson Reuters I/B/E/S.

"The entire sell side was looking for a loss," said Theodore O'Neill, managing director of Litchfield Hills Research, in a client briefing late Wednesday.

The company plans to begin selling a companion crossover model, the Model X, next year and has scheduled production of a third model - a compact electric sedan known internally as Gen 3 - in 2016.

(Reporting by Paul Lienert and Deepa Seetharaman in Detroit; Editing by Ken Wills)