Stocks surged 3 percent in a broad rally on Thursday as a long-awaited agreement by European leaders to boost the region's bailout fund promised to remove a major headwind for the market.

The agreement also strikes a deal for write-downs on Greek bonds, a source of global equity weakness over the past several months. However, optimism that a deal would be struck that would contain the crisis has led to a recent rebound.

Up more than 13 percent so far in October, the S&P 500 is on pace for its best monthly percentage gain since January 1987. The gain follows five negative months on the index.

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 while the S&P financial index soared 6.1 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.4 trillion.

There has been a risk of a very serious issue if the European financial system were to get worse, but now we have a plan in place that I believe will rectify the situation, said David Smith, chief investment officer at Rockland Trust Investment Management Group in Rockland, Mass.

Between this and some of the good news we've gotten domestically, there's clearly a scenario where strength in equities can continue into 2012, and in that case stocks look cheap.

As an example of positive domestic news, Smith pointed to the government's estimate of third-quarter economic growth, which expanded at the fastest pace in a year.

The Dow Jones industrial average was up 349.47 points, or 2.94 percent, at 12,218.51. The Standard & Poor's 500 Index was up 44.05 points, or 3.55 percent, at 1,286.05. The Nasdaq Composite Index was up 97.89 points, or 3.69 percent, at 2,748.56.

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.

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.

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.

Exxon Mobil Corp edged up 0.5 percent to $81.47 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.7 percent to $29.24, 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.