Stocks were also propelled higher by a turnaround in some of the riskier parts of the market. Internet, biotechnology and small-company stocks all rebounded after dragging the market lower in March.
Company earnings, already at record levels, continued to grind higher. Even an escalating conflict in Iraq that pushed up oil prices in June wasn't enough to stop stocks from rising.
"I'm not seeing anything that's going to derail the overall upward climb of the market," said Karyn Cavanaugh, senior market strategist with Voya Investment Management. "The economic backdrop is getting better, so companies will make even more money."
The Standard & Poor's 500 index fell 0.73 points on Monday, less than 0.1 percent, to 1,960.23, just two points from its record close of 1,962.87 set June 20.
The Dow Jones industrial average fell 25.24 points, or 0.2 percent, to 16,826.60 and posted a gain of 2.4 percent in the quarter. The Nasdaq composite rose 10.25 points, or 0.2 percent, to 4,408.18, rising 5 percent in the quarter.
Stocks flickered between small gains and losses on Monday, keeping major indexes close to record levels, as investors assessed the latest data on housing.
Home builders rose following news that the number of Americans who signed contracts to buy homes shot up in May. The National Association of Realtors said its seasonally adjusted pending home sales index rose 6.1 percent to 103.9 last month. It was the sharpest month-over-month gain since April 2010.
Gains for home builders were led by D.R. Horton, which rose 75 cents, or 3.1 percent, to $24.58.
Utility stocks also did well. The sector rose 0.8 percent, making it the biggest gainer of the 10 industry sectors that make up the S&P 500 industry.
The group has climbed 16.4 percent this year as bond yields have fallen, forcing investors to look elsewhere for income.
General Motor was among the day's losers.
Trading in the automaker's stock was briefly suspended in the afternoon after the company announced that it was recalling at least 7.6 million more vehicles dating back to 1997 to fix faulty ignition switches.
Perhaps the biggest surprise for investors in the second quarter was a strong rally in government bonds.
At the start of the year, most analysts and investors had expected bond yields to rise as the Federal Reserve gradually cut back on its economic stimulus and wound down its bond-buying program and the economy improved.
Instead, the opposite has happened. Bonds have rallied, pushing yields lower. Bonds have gained as inflation stayed low and as some investors remained skeptical about the long-term strength of the economy.
The yield on the 10-year Treasury note, which falls as bond prices rise, dropped to 2.52 percent on Monday from 2.54 percent on Friday. It had started the year at 3 percent.
Joe Hieder, a regional managing principal at Rehmann Financial, a wealth adviser, said he was surprised that long-term interest rates were still moving lower. He noted that the low rates could continue to encourage people to buy homes. They're "a benefit to everyone, potentially except the retirees that are living on fixed income."
Among other stocks making big moves:
Pittsburgh-based PPG Industries announced plans to buy Mexico's Consorcio Comex for $2.3 billion. The maker of paints wants to bolster its business in Mexico and Central America. PPG Industries rose $6.10, or 3 percent, to $210.15.
MannKind, a bio pharmaceutical company, jumped 96 cents, or 9.6 percent, to $10.96 after the Food and Drug Administration on Friday approved Afrezza, a diabetes medication to help patients control their blood sugar levels during meals.
Allergan fell after the company updated investors on the status of some of the key drugs that it is developing. The drug maker said the FDA had raised issues about a drug that is being developed as treatment of migraines in adults. The stock dropped $4.73, or 2.7 percent, to $169.20.