U.S. stocks on Thursday rallied for a second day, with all three major indexes ending up more than 1% as data showing a second consecutive quarterly contraction in the economy fueled investor speculation the Federal Reserve may not need to be as aggressive with interest rate hikes as some had feared.

The yield on benchmark 10-year Treasury notes retreated following the data, while utilities and real estate - both of which tend to rise when yields fall - were the day's best-performing S&P 500 sectors.

The decline in yields may suggest "that markets think the Fed will have to pivot and move rates lower at some point, maybe in the next 12-month period," said Mona Mahajan, senior investment strategist at Edward Jones.

"It does imply the pace of tightening will become more gradual going forward."

In addition, the growth forecast for second-quarter earnings has risen this week as more S&P 500 companies reported results and beat analyst expectations. Among them, Ford Motor Co shares jumped 6.1% after it reported a better-than-expected quarterly net income.

After the closing bell, Amazon.com <amzn.o> shares shot up more than 12% as the online retailer reported quarterly sales that beat Wall Street estimates. Amazon.com ended the regular session up 1.1%. Shares of Apple were up more than 3% after hours following the company's quarterly report and upbeat forecast, and S&P 500 e-mini futures were up 2% late.

Early in the day, the U.S. Commerce Department said the American economy unexpectedly contracted in the second quarter - the second straight quarterly decline in gross domestic product (GDP) reported by the government.

The news increased the possibility that the economy was on the cusp of a recession, and some investors said it might deter the Fed from continuing to aggressively increase rates as it battles high inflation.

The Dow Jones Industrial Average rose 332.04 points, or 1.03%, to 32,529.63 the S&P 500 gained 48.82 points, or 1.21%, to 4,072.43 and the Nasdaq Composite added 130.17 points, or 1.08%, to 12,162.59.

The Nasdaq registered its biggest two-day percentage gain since May 27.

Stocks had rallied in the previous session when the Fed raised rates and comments by Fed Chairman Jerome Powell eased some worries about the pace of rate hikes.

"More investors are getting in now because they think at least there's not going to be any big surprises over the balance of the summer," as far as rates are concerned, said Alan Lancz, president of Alan B. Lancz & Associates Inc, an investment advisory firm based in Toledo, Ohio.

The Fed on Wednesday raised the benchmark overnight rate by three-quarters of a percentage point. The move followed a 75 basis points hike last month and smaller moves in May and March, in an effort by the U.S. central bank to tamp down soaring inflation.

Investors have expressed concern that inflation and aggressive Fed rate hikes could at some point tip the economy into a recession.

Among declining stocks, Facebook and Instagram parent Meta Platforms Inc fell 5.2% after it posted its first-ever quarterly drop in revenue.

Volume on U.S. exchanges was 11.21 billion shares, compared with the 10.86 billion-share average for the full session over the last 20 trading days.

Advancing issues outnumbered declining ones on the NYSE by a 3.56-to-1 ratio; on Nasdaq, a 1.66-to-1 ratio favored advancers.

The S&P 500 posted three new 52-week highs and 31 new lows; the Nasdaq Composite recorded 67 new highs and 97 new lows.