BEIJING, China — Wall Street rallied Thursday for its first gain in three days after a sudden surge in oil prices revived beaten-down energy stocks. But, as has so often been the case in this year's market sell-off, it took a few U-turns to get there.

The price of crude spurted as much as 30% higher after President Donald Trump said he expects Russia and Saudi Arabia to back away from their price war, which erupted last month and helped drag U.S. oil to its lowest price in 18 years. The surge lifted energy stocks enough to pull the S&P 500 higher and outshine another dismal report showing that millions of Americans are joining the unemployment queue by the week.

But stocks and oil quickly pared much of their initial gains and then see-sawed through the day as markets weighed how seriously to take Trump’s statement, particularly after the Kremlin reportedly disputed part of his tweet, before climbing again to the close.

By the end of trading, the S&P 500 rose 2.3%, while U.S. oil was up $5.01, or 24.7%, after settling at $25.32 per barrel.

RELATED: Social Security recipients won't have to file tax return to get stimulus check after all

RELATED: Stimulus check calculator: See how much you'll likely be getting

“Investors are just grasping at a positive straw here on a particular day," said Phil Orlando, chief equity market strategist at Federated Hermes. “The collapse in the energy market is creating a significant amount of additional pressure on the U.S. economy, not nearly as significant as the coronavirus, but significant nonetheless.”

The market’s focus has been on oil not just because its plunge to below $20 earlier this week from $60 at the start of the year has caused stocks in the industry to more than halve. Another worry is that heavily indebted oil companies will also be forced to default, which could cause more damage in the bond market where the total amount of debt has exploded.

Producers have been continuing to pull oil from the ground to maintain their market share, even as demand for energy cratered because of widespread stay-at-home orders and other economy-damaging restrictions caused by the coronavirus outbreak. Trump tweeted Thursday that he hopes and expects cuts in production are coming after talking with Saudi Crown Prince Mohammed bin Salman.

That helped energy stocks in the S&P 500 rally 9.1%, by far the biggest gain among the 11 sectors that make up the index. Schlumberger jumped 10.2%, EOG Resources rose 10.7% and Occidental Petroleum leaped 18.9%, though all three remain down between 50% and 70% for the year.

“This is a knee-jerk reaction more than anything else,” said Willie Delwiche, investment strategist at Baird. “I don’t think it changes much of the bigger picture for what we’re going through in terms of economic uncertainty and trying to wrap our minds around the extent of the weakness we’re going to see.”

The S&P 500 rose 56.40 to 2,526.90. The Dow Jones Industrial Average gained 469.93, or 2.2%, to 21,413.44, and the Nasdaq rose 126.73, or 1.7%, to 7,487.31.

RELATED: Dr. Fauci's face will soon be on a bobblehead

RELATED: Doctors in Pennsylvania say they've developed potential coronavirus vaccine

The S&P 500 was down as much as 0.6% earlier Thursday after the U.S. government reported that more than 6.6 million Americans applied for unemployment benefits last week. That’s double the prior week’s number, which itself was nearly five times the prior record set in 1982.

Roughly one of every 16 Americans in the workforce has applied for unemployment benefits in the last two weeks, and economists expect the number only to rise further. That has many investors bracing for what may be the worst recession of their lifetimes.

The number of confirmed cases worldwide has topped 1 million, led by the United States with more than 236,000, according to a tally by Johns Hopkins University.

For most people, the coronavirus causes mild or moderate symptoms, such as fever and cough that clear up in two to three weeks. For some, especially older adults and people with existing health problems, it can cause more severe illness, including pneumonia, and death.

More than 51,000 have died, but over 208,000 have recovered.

Many investors expect markets to remain incredibly volatile until the number of new infections peaks, Only that can clear the uncertainty about how bad the upcoming downturn will be and how long it will last.

The S&P 500 is still down nearly 22% for 2020 so far, and investors are preparing for companies to soon begin reporting weaker profits from year-ago levels. Earnings reporting season for the first quarter kicks off in earnest in two weeks.

“The duration and impact of this virus remains unknown and volatility will remain the norm and not the exception,” said Terry Sandven, chief equity strategist at U.S. Bank Wealth Management.

“It’s hard to envision the market moving meaningfully higher until you get some visibility of where earnings are going to go,” he said.

To help cushion the blow, Congress last week agreed on a $2.2 trillion economic aid package and the Federal Reserve promised to buy as many Treasurys as needed to keep credit markets running smoothly.

The Fed’s moves, in particular, have helped improve trading in markets that provide lending to governments, hospitals, companies and other vital areas of the economy, investors say.