Tesla Motors Inc. TSLA -6.06% posted a surprise $22 million profit in its latest period, buoyed by record sales of its pricey electric cars and boosting Chief Executive Elon Musk’s plan to sharply lift output ahead of its release of a sedan to compete against mass-market rivals.

The Palo Alto, Calif., company reported its first profit after 12 quarterly losses amid a push to generate cash for building its $35,000 Model 3. The company has pledged to lift annual production to 500,000 cars in 2018, from about 50,000 last year.

The quarter’s profit—a record and only the second time ever—was driven higher by improved sales of the Model S sedan and Model X sport-utility vehicle, a reduction in spending and a boost from selling pollution tax credits to other auto makers. Gross profit from the credits soared to $139 million from $39 million a year ago—above UBS analyst Colin Langan’s estimate for $30 million during the quarter.

“One of the criticisms I’ve seen out there is that perhaps Q3 was at the expense of Q4—this is not true,” Mr. Musk told analysts on Wednesday. The company in the current quarter may be profitable on an adjusted basis and maybe without adjustments, he said. “We are headed to have a great fourth quarter.”

Revenue shot up to $2.3 billion from $936.8 million a year earlier. Tesla delivered 24,821 of its Model S sedans and Model X sport-utility vehicles combined, more than double the year-ago figure.

Its shares were up 5% to $212.05 in after-hours trading on Wednesday.

Tesla said it generated free cash flow, repaid $600 million in debt and finished September with $3.1 billion in cash, a decline of $162 million from the end of June.

Tesla needs about $2.5 billion through the end of 2017 for the Model 3 rollout and the completion of a huge battery factory in Nevada, according to Brian Johnson, an automotive analyst at Barclays.

The improved results also could help Mr. Musk make the case that he can handle merging Tesla with SolarCity Corp SCTY -0.60% ., which could require additional cash. The combined companies ultimately may need to raise $12.5 billion for spending through 2018, according to Oppenheimer & Co. Tesla and SolarCity shareholders are scheduled to vote on a merger Nov. 17.

Mr. Musk reiterated on Wednesday that Tesla doesn’t need to raise money this year, adding that the plan to bring out the Model 3 doesn’t include doing so. But he left the door open, noting that the modest war chest could get “a little scary in terms of how much capital we have in the bank relative to our sales volume.”

“There could be unexpected negative things that occur; there could be some global, macroeconomic slowdown…Who knows what could happen?” Mr. Musk said.

Sales were likely helped by a new $66,000 version of the Model S, analysts said. The September quarter was the first full quarter in which Tesla offered that vehicle.

Tesla also lowered its forecast for capital spending this year to $1.8 billion from $2.25 billion. About $1 billion of that spending could occur in the fourth quarter, it said.

Write to Tim Higgins at Tim.Higgins@WSJ.com