Stocks rallied on Thursday as investors were relieved Fed Chairman Ben Bernanke withstood a barrage of pointed questions from Congress on the Bank of America-Merrill Lynch deal relatively unscathed.

Retailers and home builders led stocks higher for much of the session, helped by a surprising profit increase from retailer Bed Bath & Beyond Inc. Energy companies' shares also rose as oil prices climbed above $70 a barrel.

Stocks extended gains shortly before midday as investors took Bernanke's hearing in stride. Despite lawmakers' criticisms of Bernanke, the Fed chairman is well liked on Wall Street and most analysts anticipate he will be reappointed.

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 Bank of America's CEO Ken Lewis to go through with the deal after Lewis raised objections.

There was concern as to what might happen there. He seemed to give some pretty decent testimony; at least it didn't get out of hand, said Bucky Hellwig, senior vice president at Morgan Asset Management, in Birmingham, Alabama.

The Dow Jones industrial average <.DJI> jumped 172.54 points, or 2.08 percent, to 8,472.40, snapping a four-day losing streak. The Standard & Poor's 500 Index <.SPX> gained 19.32 points, or 2.14 percent, to 920.26. The Nasdaq Composite Index <.IXIC> advanced 37.20 points, or 2.08 percent, to 1,829.54.

With the second quarter ending early next week, some portfolio managers have already begun their end-of-quarter window dressing of selling some shares with big losses and buying some of the quarter's best-performing stocks to help improve their returns. This Wall Street ritual also is likely to have helped ignite Thursday's rally.

The S&P 500 is up 36 percent from a 12-year closing low hit on March 9.

Another sign that encouraged investors was news the Federal Reserve was scaling back some emergency funding programs even while extending a number of others.

What we saw is an improvement here in that one program went away, but also the flexibility that these other ones are staying around in case they're needed, Hellwig said.

PALM POPS UP AFTER THE BELL

After the close, Palm Inc
shares rose 10.2 percent to $15.45 after the smartphone maker posted a narrower-than-expected loss despite a steep revenue decline.

On Nasdaq, Palm added 0.6 percent to close at $14.02 ahead of the results.

During the regular session, shares of energy companies gained, including Exxon Mobil Corp , up 2.1 percent at $69.88. Oil jumped $1.56 to end at $70.23.

Also boosting the market was home builder Lennar Corp , which posted a wider quarterly loss, but noted an increase in new home sales and orders. Its stock shot up 17.5 percent to $9.19.

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

Gains in home builders and retailers point to signs of strength in consumer spending, which could be a boon for stocks just as the second-quarter earnings reporting period gets under way.

In the retail sector, Bed Bath & Beyond reported a surprising increase in quarterly profit as it cut costs to offset slumping demand, and its stock gained 9.5 percent to $31.08. The S&P retail index <.RLX> climbed 3.8 percent.

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

Trading volume was below average on the New York Stock Exchange, with about 1.23 billion shares changing hands, below last year's estimated daily average of 1.49 billion, while on Nasdaq, about 2.28 billion shares traded, matching last year's daily average.

Advancing stocks outnumbered declining ones on both the NYSE and the Nasdaq by a ratio of about 4 to 1.

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