Tesla turned a surprise profit despite a plant shutdown and lower sales.

The result was achieved “despite tremendous difficulties,” said the chief executive, Elon Musk, including a plant shutdown and lower sales.