U.S. stocks rallied for a second day on Thursday as data showing a contraction in the U.S. economy for the second straight quarter raised investor speculation the Federal Reserve may not need to be as aggressive with interest rate hikes as some had thought.

Most S&P 500 sectors gained, with utilities up the most, while the yield on benchmark 10-year Treasury notes retreated.

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 have reported results and beaten analysts' expectations. Among them, Ford Motor Co shares rose after it reported a better-than-expected quarterly net income.

During the quarter, gross domestic product fell at a 0.9% annualized rate, the U.S. Commerce Department said in its advance GDP estimate. The number exceeded economists' consensus forecast in a Reuters survey for an 0.5% annualized rate.

Two consecutive quarters of declines in growth are traditionally considered a recession, but the private research group which is the official arbiter of U.S. recessions looks at a broad range of indicators, including jobs and spending.

Stocks rallied in the previous session when the Fed raised interest rates as expected 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," said Alan Lancz, president of Alan B. Lancz & Associates Inc, an investment advisory firm based in Toledo, Ohio.

According to preliminary data, the S&P 500 gained 48.64 points, or 1.21%, to end at 4,072.25 points, while the Nasdaq Composite gained 126.72 points, or 1.09%, to 12,163.72. The Dow Jones Industrial Average rose 324.66 points, or 1.01%, to 32,522.25.

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

Investors have worried that rising 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 after it posted its first-ever quarterly drop in revenue.