The Dow Jones industrial average erased a nearly 400-point loss Thursday afternoon as tailwinds from strong earnings helped the index snap a four-day losing streak.

The Dow Jones industrial average edged out a 5.17-point gain to finish at 23,930.15 by the closing bell after falling nearly 400 points in morning trading. The S&P 500 closed down 0.23 percent to close at 2,629.73 after dropping as much as 1.6 percent. Both indexes also dipped below their 200-day moving averages, but were back above those levels by the end of the session.

The Nasdaq composite fell 0.18 percent after dropping more than 1 percent, ending the day at 7,088.15. The Nasdaq pared losses later in the day was buoyed by a rebound in Amazon and Apple shares as well as a 0.6 percent gain in software giant Microsoft. Enterprise technology company Cisco added 1.3 percent.

"The market got very cheap and buyers just came in," said Jeremy Klein, chief market strategist at FBN Securities. "I mean you'll still seeing sectors like banks lag ... [but] industrials had a really nice bounce. A stock like Boeing, which has been really dragging lately, found a nice bid this morning."

Boeing — which has struggled for gains since reporting earnings last week — carried the Dow higher, with the aircraft manufacturer rallying 2 percent. Shares of fellow industrial company 3M gained 0.6 percent.

The afternoon rally came after a sharp fall in equities earlier in the session, as a batch of better-than-expected earnings failed to push the major averages higher while trade and geopolitical worries mounted.