U.S. stocks posted slight gains on Wednesday after a volatile session kicked off 2019.

The Dow Jones Industrial Average closed 18.78 points higher at 23,346.24 after dropping nearly 400 points earlier in the day. The gained 0.1 percent to close at 2,510.03 while the Nasdaq Composite climbed 0.46 percent to 6,665.94. At their lows of the day, both the S&P 500 and Nasdaq were both down more than 1 percent. Volatility was rampant in December as the S&P 500 posted its worst December since the great depression, leading to its worst year since the financial crisis in 2008.

"Finally, we're starting to see this buy-the-dip mentality creep into the market," said Phil Blancato, CEO of Ladenburg Thalmann Asset Management. "It's because of valuations and it's because the underlying data has been pretty good so far, albeit a bit softer."

"I think the market has accepted that, it's now capitulated and I think we grind higher here," Blancato said.

Facebook and Amazon rose 3.5 percent and 2.5 percent, respectively, to help the Nasdaq recover its losses. Bank shares erased earlier losses as Goldman Sachs, Bank of America and J.P. Morgan Chase all climbed more than 1 percent.

Energy stocks also contributed to the move off the lows. The S&P 500 energy sector rose 2.1 percent, led by gains in Cabot Oil and Hess, as U.S. crude surged 2.5 percent.

Stocks initially fell on Wednesday after a private sector survey showed manufacturing activity in the world's second-largest economy contracted for the first time in 19 months. China's Markit Manufacturing Purchasing Managers' Index (PMI) for December dipped to 49.7 from 50.2 in November.

Meanwhile, the euro zone manufacturing PMI remained at its lowest level since February 2016, according to IHS Markit. The data also showed confidence about the future hit a fresh six-year low. In the U.S., the IHS Markit manufacturing PMI slipped to a 15-month low in December.

"Everybody is terrified that this is a sign of a global slowdown," Art Cashin, director of floor operations at UBS, told CNBC's "Squawk on the Street." "It was only eight months ago we were talking about synchronized growth and all of that is falling apart."

The weaker-than-expected data follows a poor official survey on factory output, compounding concerns about a possible economic slowdown this year.