Tesla Inc. posted a surprise profit for the third quarter, easing investor fears that the electric car maker’s pursuit of growth and record production figures would come at the expense of the bottom line.

The company said Wednesday that it would continue to be profitable, but cautioned that new products could affect its margins.

Tesla’s...