U.S. stocks reversed sharp losses Wednesday to notch modest gains.

The turnaround followed a rebound in crude-oil prices and occurred in the wake of weak U.S. economic data.

The S&P 500 SPX, -1.11% closed 8.53 points, or 0.4%, higher at 1,929.30 with nearly all of its 10 main sectors in positive territory. Materials and energy stocks underpinned the rebound, closing up 1% and 0.9% respectively. Financial stocks were the only sector that closed in the red, down 0.2%.

The Dow Jones Industrial Average DJIA, -0.87% ended 53.21 points, or 0.3%, higher at 16,484.99, reversing a more than 250-point drop. The moves in the blue-chip gauge were led by gains in United Technologies Corp. US:UTX, up 2.2%, and Apple Inc. AAPL, -3.17% , 1.5% higher.

Meanwhile, the tech-heavy Nasdaq Composite COMP, -1.07% rose 39.02 points, or 0.9%, to 4,542.61.

“Oil is today’s primary catalyst. The correlation between equities and oil is over 90%,” said Kent Engelke, chief economic strategist at Capitol Securities Management.

“Markets are also on edge because of bank loans to the oil industry, a fear I think is vastly overblown, but the narrative is increasing in intensity,” Engelke said.

Oil futures CLJ26, settled up 0.9% but saw volatile trade after data from the U.S. Energy Information Administration showed a rise in weekly crude-oil supplies by 3.5 million barrels but also indicated a decline in crude production. Brent UK:LCOJ6 reversed earlier losses to jump 2.8%.

Oil had briefly dropped nearly 4% earlier after Saudi Arabia Tuesday dashed hopes of a production cut to tackle a supply glut in the international oil markets.

On the data front, a reading on February’s activity in the U.S. services sector dropped Wednesday to the lowest level in nearly 2½ years, offering a worrying signal that weakness in manufacturing may have spread.

“It’s supposed to be the services side of the U.S. economy that is the firewall against a U.S. recession, with the manufacturing side already in one,” said Peter Boockvar, chief market analyst at The Lindsey Group, after the data release.

But the report proved, according to Boockvar, that “the services sector is not immune to slowing global growth, a recession in manufacturing, a higher U.S. savings rate, a rising cost of corporate capital and a bear market in stocks.”

More data:Sales of new homes plummeted in January to the lowest level since October, the Commerce Department said Wednesday.

At 7 p.m. Eastern, St. Louis Fed President James Bullard was expected to talk about the economy and monetary policy at an event in New York.

Fed Vice Chairman Stanley Fischer said late Tuesday that the market selloff this year may not damage the economy, noting there have been signs of a pickup in growth and wages in the first quarter.

Movers and shakers: Chesapeake Energy Corp. US:CHK US:CHK shares reversed heavy premarket losses to close 23% higher, marking its best one-day percentage gain since 2008, according to Dow Jones data. The rally came after the company’s fourth-quarter loss widened and it unveiled further cuts to capital expenditures and asset sales.

U.S.-listed shares of major European miners slumped after Citigroup C, -1.47% cut the sector to neutral from bullish. The analysts said the recent run-up in the share prices made them more reasonably valued and “with lower growth options, our fear is share prices will be pushed down until we see a market rate of return.”

Shares of BHP Billiton PLC BHP, -1.40% UK:BLT BHP, +1.31% fell 5.4%, while Rio Tinto PLC RIO, -0.46% RIO, +0.34% RIO, +1.26% lost 3.6%.

Target Corp. TGT, +0.82% shares gained 4% after the company swung to a profit in the fourth quarter, helped by a gain on the sale of its pharmacy and clinic businesses.

Shares of First Solar Inc. FSLR, -3.51% jumped 12.4% after the solar power company late Tuesday reported quarterly results that beat forecasts.

GameStop Corp. GME, +2.93% rose 1.5% after the retailer late Tuesday said its board approved hiking the quarterly dividend to 37 cents a share from 36 cents.

Other markets: Asian stocks mostly slumped as concerns over the oil prices took a toll on sentiment. The Shanghai Composite Index SHCOMP, +2.06% bucked the negative trend and rose 0.9% after authorities guided the yuan USDCNY, lower for a second day in a row.

How to build a hunker-down ETF portfolio

European markets were covered in a sea of red. The Stoxx Europe 600 index SXXP, -0.66% fell 2.3% to 320.23, its lowest close since Feb. 12.

In currencies, the pound continued its rout on concerns over the so-called Brexit vote in June. Against the dollar GBPUSD, -0.42% , the currency dropped below $1.40 for the first time since March 2009.

Read: Rough times still ahead for the pound after sliding toward seven-year lows

Gold prices US:GCJ6 climbed over 1%, and Treasury yields rose as stocks recovered. The yield on the 10-year Treasury note TMUBMUSD10Y, 0.701% , the Treasury market’s benchmark, at one point tumbled to its lowest level in a year.

--Sara Sjolin in London contributed to this article.