U.S. stocks erased steep losses Wednesday to close mostly higher, after the S&P 500 briefly descended into negative territory for the year and a second day of weakness for the Chinese yuan continued to send shock waves through global markets.

Traders unloaded telecommunication and financial stocks as worries about China—the world’s second-largest economy—continued after the country intervened on Wednesday to manage the devaluation of the yuan USDCNY, -0.17% .

On Tuesday, the People’s Bank of China set in motion what the central bank described as a pledge to let market forces dictate the currency’s value.

Beijing’s actions have had spillover effects on global markets, raising concerns about the economic health of the country, which is one of the largest importers of goods and buyers of services.

The Dow Jones Industrial Average DJIA, -1.84% was virtually flat, closing down 0.33 points at 17,402.51, after having been off by as many as 277 points. Technically, the blue-chip index logged its ninth decline of the past 10 trading days. The last time the index suffered a loss in nine of 10 trading sessions was from May 7 to May 18, 2012, according to Dow Jones data.

Session moves of 200 points or more on the Dow have been running well above average this year, accounting for nearly one-fifth of trading days so far. At 27 days with point swings of 200 points or more, 2015 so far has been the most volatile year for the Dow since 2011, which saw 37 trading days where the index closed up or down more than 200 points, according to Dow Jones data.

The S&P 500 SPX, -1.15% finished up 1.98 points, or 0.1%, at 2,086.05, after being off by as many as 32 points and venturing into negative territory for the year. The swing higher for the S&P 500, on a percentage basis, was the biggest positive reversal for the index since Oct. 4., 2011, according to Dow Jones data.

The Nasdaq Composite Index COMP, -0.13% closed up 7.60 points, or 0.2%, at 5,044.39, after being down as many as 91 points. The tech-heavy Nasdaq spent much of the day struggling to clear the psychologically significant level of 5,000 reached back in March.

In the absence of any big change to China’s stance, investors were likely looking at the S&P 500’s near-term support level of 2,050 to stage a turnaround, said Mark Luschini, chief investment strategist at Janney Montgomery Scott, in an interview. The S&P 500 hit an intraday low of 2,052.09 before bouncing back.

“There was pretty violent selling on news that was no longer new, so maybe [China’s move] was not as nefarious as it was initially thought,” Luschini said.

Uncertainty over the extent of China’s currency actions remains a big concern for investors in an already struggling U.S. stock market.

“The full implications will take some time to play out, ultimately, because we don’t know the extent to which China might let its currency depreciate,” said Dan Greenhaus, chief strategist at BTIG, in emailed comments. “More generally, it’s clearly a weak period for U.S. equities and China’s news is just piling on,” he said.

The main stock benchmarks suffered from sharp selling pressure on Tuesday, with the Dow tumbling more than 200 points, the S&P 500 index dropping 1%, and the Nasdaq Composite Index falling 1.3%. The losses came after the People’s Bank of China’s currency-devaluation maneuvers.

The PBOC said Tuesday’s decision was a one-off move, but on Wednesday the PBOC allowed the yuan to fall further, essentially devaluing the currency for a second consecutive day.

A weaker yuan is good for Chinese exporters but bad for international companies that compete with Chinese companies or export goods to the world’s No. 2 economy, including car makers, luxury-goods firms and technology companies.

Apple Inc. shares AAPL, +3.03% swung to a 1.5% gain, reversing a more than 3% decline on the day. Apple posted a 5.2% decline Tuesday on China’s currency moves. The yuan devaluation makes the iPhone maker’s gadgets more expensive for Chinese buyers.

See:The 20 China-exposed stocks to avoid

Shares of other technology companies like chip maker Micron Technology Inc. MU, -3.13% and Qualcomm Inc. QCOM, +1.11% closed with slight gains, after paring earlier losses.

Luxury-handbag makers including Salvatore Ferragamo SpA UA:SFER, which fell on Tuesday in the wake of the China news fell for a second day.

Analysts speculate that the yuan moves could force the Federal Reserve to delay its first rate hike.

Data:

Job openings, the number of available jobs in June, fell even as companies hired more. There were 5.25 million job openings, down from 5.36 million in May.

On Wednesday, the Treasury Department said the U.S. was running a $149 billion budget deficit in July.

Earnings: Shares of Macy’s Inc. M, -6.65% fell 5% after the company cut its outlook.

Alibaba Group Holding Ltd. shares BABA, +0.51% slumped 5% after the Chinese e-commerce giant reported weaker-than-expected revenue for its fiscal first quarter.

Movers and shakers: Shares of Warren Buffett’s Berkshire Hathaway Inc. BRK.B, -2.37% BRK.A, -2.46% slipped after Standard & Poor’s Ratings Services placed the company’s ratings on review following news the conglomerate will buy Precision Castparts Corp. US:PCP for about $32 billion. S&P’s move reflects, in part, uncertainty over how Berkshire will finance the deal.

Other markets: Crude oil CLU25, erased earlier losses and settled higher after the International Energy Agency said demand for oil is increasing at its fastest pace in five years, boosted by an oil-price drop below $50 a barrel. Gold prices moved higher as the metal drew haven bids.

The ICE dollar index DXY, -0.02% was down 1% as investors digested the continued slide in the Chinese yuan.

European markets were also sharply lower on the yuan news, with the Stoxx Europe 600 index SXXP, -3.24% off nearly 3%.

Asian stock markets closed lower almost across the board.

—Sara Sjolin in London contributed to this article.