Major indexes headed higher at the opening bell, following solid gains in Europe, then kept climbing throughout the afternoon. Nearly three stocks rose for every one that fell on the New York Stock Exchange, and every sector in the S&P 500 finished with gains.
Nine hours after a self-imposed deadline passed, European officials announced the breakthrough on Greece early Monday.
The tentative agreement removed an immediate threat that the country would default on its debts and leave the euro. In exchange for a three-year loan program, the deal requires Greece's parliament to pass tax increases and other key demands from its lenders into law by Wednesday.
"Our markets have reason to cheer," said Tim Dreiling, senior portfolio manager at the U.S. Bank's Private Client Reserve. "It's a reprieve from worry for a few days at least."
The Standard & Poor's 500 index gained 22.98 points, or 1.1 percent, to 2,099.60.
The Dow Jones industrial average climbed 217.27, or 1.2 percent, to 17,977.68, while the Nasdaq gained 73.82, or 1.5 percent, to 5,071.51.
Major markets in Europe rallied on the news. Germany's DAX climbed 1.5 percent and France's CAC 40 surged 1.9 percent. Britain's FTSE 100 finished with a gain of 1 percent.
Worries over Greece and China have buffeted markets in recent weeks. Barring any worrying news out of either country, investors will likely shift their attention to earnings reports as a parade of major corporations turn in second-quarter results.
JPMorgan Chase, Johnson & Johnson and Wells Fargo will report early Tuesday, followed by Bank of America and Google later in the week. Analysts expect overall earnings to fall 4.5 percent compared with the prior year, according to S&P Capital IQ. If that forecast comes true, it would mark the first drop in earnings since 2009.
Among other companies making big moves on Monday, Marathon Petroleum soared 8 percent, the biggest gain in the S&P 500, following its announcement that a partnership it runs will buy MarkWest Energy Partners, a company that works with natural gas. Marathon jumped $4.29 to $58.78.
Microsoft said it would roll out Windows 10 in late July. The upgraded operating system is supposed to allow users to switch seamlessly between personal computers and their gadgets. The company's stock rose 93 cents, or 2 percent, to $45.54, among the biggest gains in the Dow.
In Asia, Japan's Nikkei 225 gained 1.6 percent, South Korea's Kospi gained 1.5 percent. In China, the Shanghai Composite added 2.4 percent, bouncing back after a slew of government measures to halt a dramatic slide. Hong Kong's Hang Seng rose 1.3 percent.
Back in the U.S., government bond prices slipped, pushing yields up. The yield on the 10-year Treasury note rose to 2.43 percent from 2.40 percent late Friday.
Precious metals finished with slight losses. Gold lost $2.50 to settle at $1,155.40 an ounce, while silver sank 3 cents to $15.44 an ounce. Copper picked up a penny to close at $2.56 a pound.
Benchmark U.S. oil fell 54 cents to close at $52.20 a barrel on the New York Mercantile Exchange. Brent crude, an international benchmark, fell 88 cents at $57.85 a barrel in London.
In other futures trading on the NYMEX:
— Wholesale gasoline fell 7.7 cents to close at $1.940 a gallon.
— Heating oil fell 2.1 cents to close at $1.719 a gallon.
— Natural gas rose 9.4 cents to close at $2.864 per 1,000 cubic feet.