WASHINGTON—The U.S. economy posted its best six-month stretch of growth in three years despite two hurricanes, a sign that it might be breaking out of its long-running slow-growth trend, with the help of soaring stock prices and rising business and consumer confidence.

Gross domestic product, the broadest measure of goods and services produced in the U.S., expanded at a 3% annual rate in the third quarter, the Commerce Department said Friday. That followed 3.1% annual growth in the spring.

Though...