The Dow Jones industrial average rose 29.82 points and broke an eight-day losing streak. The Dow was down as much as 166 points in the morning. A ninth day of losses would have been the longest for the blue-chip index since February 1978.
Markets have fallen recently because investors are becoming increasingly worried about the U.S. economy.
Shortly after the market opened, the Institute of Supply Management said its index measuring the service sector of the U.S. economy grew in July at the weakest pace in 17 months. Economists had expected a slight increase.
The report was the latest sign over the last week that the economy may be slowing. Consumer cut their spending in June for the first time in nearly two years; manufacturing slowed, and the government said that in the first half of the year the economy grew at its slowest pace since the recession ended in June 2009.
"There has been too much at the same time for investors to hang in there and you're starting to see some element of panic finally showing up," said Andrew Goldberg, U.S. market strategist at JP Morgan Funds.
The Dow, the Standard & Poor's 500 index and Nasdaq were down more than 1 percent earlier in the day, but edged higher throughout the afternoon.
The Dow Jones industrial average finished with a gain of 0.3 percent, to 11,896.44. The S&P 500 index rose 6.29, or 0.5 percent, to 1,260.34. The S&P had been down for seven straight days through Tuesday. It is up 0.2 percent for the year after being down 0.3 for the year on Tuesday.
The Nasdaq composite added 23.83, or 0.9 percent, to 2,693.07.
The broad S&P 500 index- the index followed by most professional money managers and U.S. mutual funds- rose after it hit a low for the year of 1,234. Some investors saw it as an opportunity to buy the S&P 500 index. As a whole, companies in the index are expected to have record profits this year.
Some of those gains might also be due to automatic buying triggered when an index reaches a certain level. Many traders use computer programs that buy or sell stocks once they break through their long-term averages.
"It seems like the early money was based on fear and the market climbed back as computer-program trading took over," said Mark Lamkin, the head of Lampkin Wealth Management in Louisville, Kentucky.
Lamkin said the stock market was in a "tug of war" between strong corporate earnings and a "horrible economic backdrop."
Coca-Cola led the Dow average higher with a gain of nearly 2 percent. Companies that depend most on an expanding economy in order to make profits had the steepest losses. Caterpillar Inc. fell 0.9 percent, the most of the 30 stocks in the Dow average, followed closely by Chevron Corp. and Boeing.
Along with the concerns about the U.S. economy, investors were also unnerved by a surge in bond yields to 14-year highs for Italy and Spain. High bond yields typically indicate that investors believe there is a greater chance that a country or corporation will be unable to make interest payments.
"We've been so focused inwardly because of the debt ceiling debate that we've ignored Europe over the last couple of weeks," said J.J. Kinahan, chief options strategist at T.D. Ameritrade. "We have problems, but if Italy falls the euro zone doesn't look sustainable."
Italy and Spain are the third and fourth largest economies in Europe, respectively.
The yield on the 10-year Treasury note fell to another low for the year of 2.56 percent, from 2.62 percent Tuesday, as investors moved money into assets that hold up better during economic downturns. Gold, another traditional safe haven, rose nearly 2 percent to $1,671 an ounce.
Several large U.S. companies reported earnings before the market opened. MasterCard rose nearly 14 percent after the company beat analysts' estimates. Clorox fell 2 percent after the company said higher commodity costs were eating into its income. And CBS gained 1.6 percent after it said a deal with Netflix Inc. had lifted profits.
Payroll processor ADP said private companies added 114,000 jobs last month. The number was within Wall Street's forecasts, but still well below the rate of growth that signifies a healthy jobs market. ADP's employment figures do not always predict the government's broader employment report, which will be released Friday morning. Last month, for example, ADP reported that private employers added 157,000 jobs in June. The government later said that private companies added just 57,000 jobs.
Economists expect that 90,000 were created in the U.S. last month. That's fewer than the 125,000 jobs per month that are needed just to keep up with population growth. At least 250,000 jobs need to be created every month to substantially bring down the unemployment rate.
Analysts predict that the unemployment rate was 9.2 percent in July, unchanged from the month before.