By Gunjan Banerji and Anna Hirtenstein

U.S. stocks rallied Tuesday on fresh data showing consumer spending rebounded in May, the latest sign that the worst of the economic shock from the pandemic may have passed.

Retail sales rose 17.7% in May, according to data from the Commerce Department, marking a sharp bounceback from a record slump in March and April during the depths of the pandemic. May's jump was the biggest monthly increase in retail spending in records dating back to 1992.

"It's a blowout number," said Sebastien Galy, a macro strategist at Nordea Asset Management. "This is a very sharp rebound and is very encouraging for the U.S. and the global economy as a whole. While we still might not see a V-shaped recovery, we're certainly moving away from a U-shaped one."

The S&P 500 gained 58.15 points, or 1.9% to 3124.74, capping off a third consecutive session of gains. The Dow Jones Industrial Average climbed 526.82 points, or 2%, to 26289.98. The Nasdaq Composite added 169.84 points, or 1.7%, to 9895.87.

Gains in the stock market were broad, with all 11 of the S&P 500's groups rallying. The tech-heavy Nasdaq notched its third consecutive gain of at least 1%, the longest such streak since January 2019. The Russell 2000, which tracks smaller companies, rose at least 2% for the third straight session, the longest such streak since 2010, according to Dow Jones Market Data.

Shares of retail giants were some of the biggest gainers. Shares of Kohl's added $2.04, or 9%, to $24.82. Nordstrom shares jumped $2.23, or 13%, to $19.50. Gap's stock added 89 cents, or 8.5% to $11.38.

Parsing the fallout from a global pandemic has been challenging for economists and analysts. Both Tuesday's retail sales data and last month's jobs report were much stronger than projected, spurring dramatic rallies in stocks. Investors in recent weeks have also been quick to look past dour economic data and pounce on hints of a recovery.

That sentiment has helped major U.S. indexes rally more than 35% from their lows in March, a speedy rebound that has defied expectations.

"The market's still looking ahead -- past the current data, whether it's positive or negative," said John Zaller, chief investment officer at MAI Capital Management.

At the same time, volatility and some apprehension among investors has rekindled in recent days. U.S. stocks have logged some of the biggest swings in months as investors have weighed the uptick in cases and harsh reality of the downturn alongside the economic reopening across the country.

On Tuesday, stocks pared much of their gains in early trading before turning higher again. Media reports said coronavirus cases in Beijing are rising again. Recently, there have been increases in coronavirus cases in more than a dozen states.

And on Monday, U.S. stocks staged a dramatic reversal as the Fed said it would broaden a program to purchase bonds of U.S. companies.

Many expect the gyrations to continue. Some investors have been sitting on the sidelines of the market, flush with a record amount of cash.

"What makes it even more difficult now is the uncertainty around whether Covid-19 cases are going to stay at these fairly high levels," said Mr. Zaller. "That is something that is very hard to gauge."

The big intraday moves alongside swings in different sectors highlight the uncertainty permeating the market right now.

The market "is hypersensitive right now to any sort of negative news given how much of a bounceback we've had," said R.J. Grant, director of equity trading at KBW.

And many analysts are wary that the U.S. economy could be scarred by the downturn for years to come. Federal Reserve Chairman Jerome Powell said in testimony presented to the Senate Banking Committee Tuesday that the economy faces significant long-term damage from higher unemployment, despite recent signs of improvement. He said a full economic recovery is unlikely "until the public is confident that the disease is contained."

Many have attributed the stock market gains to the Fed's support this year. In addition, with bond yields hovering near lows, investors have found few attractive places to park their money. That makes them more willing to focus on a potential recovery rather than the current downturn, analysts said.

Investors have also turned to some of the momentum-driven stocks that were popular earlier in the year. Tesla's stock recently jumped above $1,000 and is sitting on gains of about 135% this year. The iShares Edge MSCI U.S.A. Momentum Factor Exchange-Traded Fund, has added about 2.2% this year, while the S&P 500 has fallen 3.3%.

"There's no where else for the cash to go," said Chris O'Keefe, a portfolio manager at Logan Capital Management. "People are searching for yield and searching for income."

The yield on the 10-year U.S. Treasury note, which is seen as a haven, rose to 0.754%, from 0.701% Monday. Yields rise as bond prices fall.

The pan-continental Stoxx Europe 600 advanced 2.9%.

In Asia, regional benchmarks rallied. South Korea's Kospi Composite gained 5.3% and Japan's Nikkei 225 climbed 4.9%. Benchmarks in Hong Kong and Shanghai also advanced.

Joanne Chiu contributed to this article.

Write to Gunjan Banerji at Gunjan.Banerji@wsj.com and Anna Hirtenstein at anna.hirtenstein@wsj.com