U.S. stocks rallied on Thursday on investors' relief that Federal Reserve Chairman Ben Bernanke was weathering a tough grilling in Congress relatively well.

Consumer discretionary shares had led stocks higher from early in the session, on positive news from the retail and home-building sectors that triggered a rally in the shares of home builder Lennar Corp and retailer Bed, Bath & Beyond .

The U.S. House of Representatives Oversight and Government Reform Committee questioned Bernanke on the Fed's role in Bank of America's takeover of Merrill Lynch, and whether he pressured BofA's CEO Ken Lewis to go through with the deal after Lewis raised objections.

As the testimony wore on, however, analysts' concerns faded and stocks sharply extended gains.

There could have been a little apprehension ahead of this that Bernanke truly was in on forcing Bank of America (to buy Merrill), but that seems to have been blown a little out of proportion, said Ryan Detrick, senior technical strategist at Schaeffer's Investment Research in Cincinnati, Ohio.

Nothing out of the ordinary is being reported so we're getting a relief rally.

The Dow Jones industrial average <.DJI> gained 138.15 points, or 1.66 percent, to 8,438.01. The Standard & Poor's 500 Index <.SPX> rose 15.64 points, or 1.74 percent, to 916.58. The Nasdaq Composite Index <.IXIC> added 29.20 points, or 1.63 percent, to 1,821.54.

Home builder Lennar Corp posted a wider quarterly loss, but reported an increase in new home sales and orders. Its stock shot up 14.2 percent to $8.94.

The Dow Jones U.S. home construction index <.DJUSHB> jumped 4.1 percent.

Retailer Bed Bath & Beyond Inc reported a surprising increase in quarterly profit as it cut costs to offset slumping demand, and its stock soared 10 percent to $31.24.

Another bright spot among retailers was J.C. Penney Co Inc , up 6.1 percent at $28.223 after JPMorgan raised its rating on the stock to overweight from neutral.

The S&P retail index <.RLX> shot up 3.4 percent.

The broad S&P 500 has climbed as much as 40 percent from March's 12-year closing low on hopes the economy was stabilizing, but jitters over the strength of a potential recovery have stalled the rally recently. The S&P 500 is up about 36 percent from that trough.

(Additional reporting by Leah Schnurr; Editing by Jan Paschal)