U.S. stocks rose modestly on Wednesday after days of selling produced a technical rebound and investors were lured by attractive share prices.

Stocks reversed losses on manufacturing data pointing to a slowing in the U.S. economic recovery.

Energy shares were among the best performers, gaining on higher oil prices. Homebuilders also advanced.

The S&P 500 index hit its lowest level on an intraday basis since April 19 after closing below its 50-day moving average for a second straight day on Tuesday.

The benchmark index had fallen for three consecutive sessions, dropping 2 percent over that time, with market internals showing declining stocks outnumbering advancers.

It's more of a technical bottom -- the constant selling just got exhausted, said Rick Meckler, president of investment firm LibertyView Capital Management, in New York.

You've had an awful lot of consecutive days of hard selling. It seems like, at least temporarily, that has come to an end.

Bespoke Investment Group said breadth in the S&P 500 was very close to extremely oversold levels, noting the last time those levels were reached on March 16 turned out to be an attractive buying opportunity.

The Dow Jones industrial average <.DJI> gained 36.90 points, or 0.30 percent, to 12,393.11. The Standard & Poor's 500 Index <.SPX> added 4.40 points, or 0.33 percent, to 1,320.68. The Nasdaq Composite Index <.IXIC> rose 13.85 points, or 0.50 percent, to 2,760.01.

Higher oil prices lifted energy companies Chevron Corp gained 1 percent to $103.27 and Exxon Mobil Corp added 1 percent to $82.13. The PHLX oil service sector index <.OSX> rose 2.2 percent.

Homebuilders also advanced after luxury builder Toll Brothers Inc said orders rose in the latest quarter as low home prices induced its target market of the affluent to start buying again. The stock climbed 3.5 percent to $20.98. The Dow Jones U.S. home construction index <.DJUSHB> advanced 1.9 percent.

New orders for long-lasting durable goods posted their largest decline in six months in April as aircraft and motor vehicle orders tumbled, a government report showed.

Recent weak U.S. data, including soft manufacturing figures from the Atlantic region and disappointing New York and Philadelphia Fed manufacturing surveys, pointed to a slowdown in the pace of economic growth.

American International Group Inc shares fell 3.7 percent to $28.36, below the $29 offer price of the 300 million shares being sold by the U.S. Treasury and the bailed-out insurance company.

(Reporting by Chuck Mikolajczak; Editing by Kenneth Barry)