U.S. stocks surged more than 2 percent on Thursday after European leaders reached a long-awaited agreement to boost the region's bailout fund and struck a deal on Greek bonds, which promised to remove a major headwind for the market.

The S&P 500 was on pace for its best monthly percentage gain since January 1987 on optimism that European leaders were nearing a resolution of the sovereign debt crisis that could end market uncertainty and drive stocks for the near term.

As a result of the fact that we've potentially taken euro Armageddon off the table there is sort of a refocusing on domestic fundamentals, which frankly have been pretty good over the last month, said Phil Orlando, chief equity market strategist at Federated Investors in New York.

For example, he cited the government's estimate of third-quarter economic growth, which expanded at the fastest pace in a year.

You've got a lot of folks that were defensively positioned, many of whom were short different aspects of the market and they've got to reverse those bearish bets in light of what is actually happening, Orlando said.

The Dow Jones industrial average gained 302.39 points, or 2.55 percent, to 12,171.43. The Standard & Poor's 500 Index jumped 36.27 points, or 2.92 percent, to 1,278.27. The Nasdaq Composite Index climbed 76.26 points, or 2.88 percent, to 2,726.93.

The gains on the S&P 500 broke the benchmark index out of a trading range between 1,230-1,250 and was just above the 200-day moving average of 1,274, viewed as the next significant technical resistance level.

Financials were the best performers, with JPMorgan Chase & Co up 7.2 percent to $36.63 and Citigroup Inc jumping 8.6 percent to $33.85. The KBW Bank index shot up 5.5 percent.

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.0 trillion euros ($1.4 trillion).

Analysts see the European developments removing risk to the U.S. economy and tamping down fears of it spilling over into the global financial system, although some felt it may be prudent to book profits from the recent rally.

We've almost recovered the whole summer slide .... OK, you've recovered your loss, this may be a great time to take some money off the table, Scott Armiger, portfolio manager at Christiana Trust in Delaware.

It won't happen. Most people will pile on now and probably get disappointed.

Exxon Mobil Corp edged up 0.3 percent to $81.30 after the U.S. oil and gas major 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.4 percent to $29.16, 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.

(Additional reporting by Angela Moon and Jennifer Ablan; editing by Kenneth Barry)