Stocks surged 3 percent on Thursday as an agreement by European leaders to help contain the region's two-year debt crisis lifted a cloud hovering over markets.

Optimism that a deal would be struck to prevent widespread financial distress fueled the market's rebound in October. The S&P 500 is up more than 13 percent this month, on pace for its biggest monthly gain since October 1974.

But some traders said implementing the agreement will present major challenges, observing that the devil is in the details.

After more than eight hours of talks, European heads of state, the International Monetary Fund and bankers sealed a deal that also foresees a recapitalization of hard-hit European lenders and a leveraging of the bloc's rescue fund to give it firepower of $1.4 trillion.

The agreement includes provisions for write-downs on Greek bonds, though decisions on how to recapitalize hard-hit European banks and boost the EU's rescue fund have not been finalized.

People had limited expectations for the leadership to do something decisive, and if the market is correct, this is a game changer that will prove bullish for the market down the road, said Robert Schaeffer, a money manager at Becker Capital Management in Portland, Oregon.

The Dow Jones industrial average was up 339.51 points, or 2.86 percent, at 12,208.55. The Standard & Poor's 500 Index was up 42.59 points, or 3.43 percent, at 1,284.59. The Nasdaq Composite Index was up 87.96 points, or 3.32 percent, at 2,738.63.

The day's gains lifted the S&P 500 above its 200-day moving average for the first time since the beginning of August, a sign of an improving trend for stocks after five straight months of losses.

It was the strongest day for volume since October 4, and the rise above the 200-day moving average may pull more long-term buyers into the market in coming days. About 11.95 billion shares traded on the New York Stock Exchange, the American Stock Exchange and Nasdaq, well over last year's daily average of 8.47 billion.

We are rallying today because the active players, mostly hedge fund managers and tactical investors, have been very neutral to even short until now. The market is up a lot, but they are rushing into getting long because they are capitulating, said James Dailey, portfolio manager of TEAM Asset Strategy Fund in Harrisburg, Pennsylvania.

Financials were the best performers, with JPMorgan Chase & Co up 8.3 percent to $37.02 and Citigroup Inc jumping 9.7 percent to $34.17. The KBW Bank index shot up 6 percent while the S&P financial index soared 6.2 percent.

Analysts see the European developments removing risk to the U.S. economy and tamping down fears of the crisis spilling over into the global financial system. The CBOE Volatility index shed 14 percent.

All 10 S&P sectors rose by more than 1 percent. Materials and energy shares were among the top gainers as the resolution in Europe allayed fears about how weak growth might impact demand. Crude oil rose 4.3 percent.

In a positive sign for the U.S. economy, the government's estimate of third-quarter growth expanded at the fastest pace in a year.

Between the deal in Europe and the GDP data, there's clearly a scenario where strength in equities can continue into 2012, and in that case stocks look cheap, said David Smith, chief investment officer at Rockland Trust Investment Management Group in Rockland, Mass.

After regular trading, insurer MetLife Inc reported third-quarter earnings that topped analysts' forecasts, sending its shares 2.6 percent higher to $36.60. Baidu Inc climbed 8.2 percent after the bell on the Nasdaq after its results.

Exxon Mobil Corp rose 1 percent to $81.88 after the Dow component said profit rose 41 percent in the third quarter, helped by higher crude oil prices and refining margins.

Dow Chemical Co's quarterly profit narrowly missed expectations. Still, the stock rose 8.2 percent to $29.10, along with the broader market.

Of 262 companies in the S&P 500 that have reported quarterly earnings, 72 percent topped Wall Street expectations, according to Thomson Reuters data.

About 87 percent of stocks on the New York Stock Exchange closed higher while 81 percent of Nasdaq issues ended in positive territory.

(Additional reporting by Chuck Mikolajczak; Editing by Kenneth Barry)