Wall Street capped its best quarter since 1998 Tuesday with more gains, a fitting end to a stunning three months for investors as the market screamed back toward its record heights after a torrid plunge.

The S&P 500 climbed 1.5%, bringing its gain for the quarter to nearly 20%. That rebound followed a 20% drop in the first three months of the year, the market's worst quarter since the 2008 financial crisis. The plunge came as the coronavirus pandemic ground the economy to a halt and millions of people lost their jobs.

“It’s the first time you’ve had back-to-back (quarters) like this since the 1930s,” said Willie Delwiche, investment strategist at Baird. “It’s pretty unprecedented.”

The whiplash that ripped through markets in the second quarter came as investors became increasingly hopeful that the economy can pull out of its severe, sudden recession relatively quickly. The hopes looked prescient after reports during the quarter showed that the job market swung back to growth and retail sales rebounded as governments relaxed lockdown orders meant to slow the spread of the coronavirus.

Stocks built on gains made toward the tail end of the first quarter, when promises of massive amounts of aid from the Federal Reserve and Capitol Hill helped put a floor under the market. Low interest rates generally push investors toward stocks and away from the low payments made by bonds, and the Federal Reserve has pinned short-term interest rates at their record low of nearly zero.

But most of Wall Street says not to expect anything close to a repeat of the rocking second quarter. A rise in infections has several states pausing their lifting of restrictions. The surge in confirmed new cases, which has prompted the European Union to bar U.S. travelers from entry, is seeding doubts that the economic recovery can happen as quickly as markets had forecast.

On Tuesday Dr. Anthony Fauci, the nation’s top infectious-disease expert, warned that the number of daily new reported infections could surge to 100,000 if Americans don’t start following public health recommendations.

Beyond the coronavirus, analysts also point to the upcoming U.S. elections and other risks that could upset markets. If Democrats sweep Capitol Hill and the White House, which many investors see as at least possible, it could mean higher tax rates, which could weaken corporate profits.

The S&P 500 gained 47.05 points to 3,100.29 on Tuesday. The Dow Jones Industrial Average rose 217.08 points, or 0.9%, to 25,812.88. It had briefly been down 120 points. The Nasdaq composite climbed 184.61 points, or 1.9%, to 10,058.77.

The S&P 500 has rallied back to within nearly 8.4% of its record set in February, after being down nearly 34% in late March. At one point earlier this month, it had climbed as close as 4.5%.

Technology, health care and financial companies powered much of the market’s broad gains Friday. The buying accelerated after a report showed stronger-than-expected improvement in consumer confidence this month.

“Broadly speaking, the market is reacting to economic data that is better than expected,” said Brent Schutte, chief investment strategist at Northwestern Mutual Wealth Management.

Schutte said the market is being supported by the likelihood that there won’t be a nationwide shutdown again, aggressive monetary policy and hopes for a vaccine sooner rather than later. “The path of least resistance is still two steps forward, one step back,” he said.

Crude oil has had a similar rebound as stocks through the second quarter, though it’s still well below where it was before the pandemic struck.

A barrel of U.S. crude oil slid 43 cents to settle at $39.27 Tuesday, but it’s still nearly double where it was at the end of the first quarter. It’s also in a different world from April, when prices in one corner of the U.S. crude market briefly went below zero amid worries that collapsing demand would leave nowhere to store all the unused oil. Brent crude oil fell 56 cents to settle at $41.15 a barrel.

The yield on the 10-year Treasury rose to 0.66% from 0.63% late Monday. It too has rallied back from its lows when recession worries were at their height. It set a record low in March when it briefly dipped below 0.50%, according to Tradeweb. The yield tends to move with investors' expectations for the economy and inflation.

European stocks closed mixed, and Asian markets finished higher.