Tesla (TSLA -1.92%) caught investors off guard last Wednesday by reporting a massive third-quarter profit of $312 million. This was just the third time in the company's history that it has reported a quarterly profit under GAAP accounting rules, and it easily set a new earnings record for Tesla.

That said, the mere fact that the electric vehicle pioneer turned profitable was not especially shocking. While analysts had on average expected a small loss, Tesla had previously projected that it would be profitable for the quarter. Yet there were plenty of surprises in the earnings report. Here are three big ones.

Model 3 gross margin surges past expectations

Three months ago, Tesla reported that Model 3 gross margin was "slightly positive" in the second quarter. At the time, it forecast that Model 3 gross margin would improve to around 15% in the third quarter and around 20% in the fourth quarter, on the way to a long-term goal of 25%.

Tesla began delivering more expensive all-wheel-drive versions of the Model 3 last quarter. It also increased the production rate. These two factors clearly supported a big improvement in gross margin. Yet with the mix of Model 3 cars certain to gravitate toward cheaper versions over time, it was hard to see a path from Tesla's projected Q3 Model 3 gross margin of 15% to the long-term goal of a 25% gross margin -- even allowing for further manufacturing cost reductions.

However, Tesla at least partially addressed this concern by reporting Model 3 gross margin in excess of 20% for the third quarter. This quickly put the company close to reaching its long-term margin target for its newest vehicle. The margin outperformance was particularly notable because it represented a clean break from Tesla's track record of repeatedly missing its forecasts.

A silver Tesla Model 3 on a road, with a green field in the background

Tesla's gross margin for the Model 3 surpassed management's guidance. Image source: Tesla.

Tesla will still need to achieve substantial cost reductions to offset the introduction of cheaper Model 3 options, but the 25% long-term gross margin target looks a lot more plausible now than it did a week ago.

Operating expenses decline

The unexpectedly high gross margin result for the Model 3 was the most important factor in Tesla's earnings beat. However, strong operating expense control also played a big role.

In the second quarter, while research and development (R&D) spending rose less than 5% year over year, Tesla's selling, general, and administrative (SG&A) expenses surged 40% year over year and 9% sequentially. Considering that Tesla's vehicle deliveries doubled in the third quarter compared to the second quarter, it would have been natural to expect another big step-up in SG&A expense last quarter.

Instead, SG&A expense declined 3% sequentially to $730 million. On a year-over-year basis, SG&A was up just 12%. Tesla also reported a sequential decline of 9% and a year-over-year increase of just 6% on the R&D line. Tesla attributed the strong cost performance to recent cost-cutting actions and having already completed most of the development work for the Model 3.

Tesla's Model 3 gross margin beat may have been the main factor in bringing the company to profitability last quarter, but spending discipline was the difference between eking out a profit and reporting blowout earnings.

Incredible free cash flow

Lastly, Tesla's cash flow performance was even more impressive than its earnings. Operating cash flow surged to $1.4 billion in the third quarter after being negative in the first two quarters of the year. Previously, Tesla had never generated more than roughly $500 million of operating cash flow in a quarter.

TSLA Cash from Operations (Quarterly) Chart

Tesla Cash from Operations (Quarterly), data by YCharts.

Free cash flow reached $881 million last quarter, which was substantially higher than Tesla's net income. Part of the discrepancy came from the $205 million in share-based compensation that Tesla handed out, while working capital improvements also made a modest contribution.

However, Tesla's strong free cash flow primarily reflected rock-solid operating performance. That suggests there was more to its big earnings beat than accounting tricks. It also means that Tesla's goal of remaining profitable in Q4 -- and hopefully in 2019 as well -- could be within reach.