By Dan Strumpf 

U.S. stocks closed higher, with the Dow and S&P 500 rebounding from their biggest losses of the year, as a last-minute diplomatic scramble got under way to keep Greece from defaulting on its debt payments.

The Dow Jones Industrial Average gained 23.16 points, or 0.1%, to 17619.51, while the S&P 500 index added 5.48 points, or 0.3%, to 2063.12. The Nasdaq Composite Index rose 28.40 points, or 0.6%, to 4986.87.

The gains closed out a ho-hum quarter for the U.S. stock market, leaving the Dow with a loss of 0.9% for the second quarter and the S&P 500 with a decline of 0.2%. The S&P 500 snapped a nine-quarter winning streak.

Stocks on Tuesday swung in a wide range, with the Dow falling as much as 20 points at midday before rebounding into the afternoon. The gains came one session after the biggest loss of the year for major U.S. stock indexes, as investors grappled with the prospect of Greece defaulting on debt payments and, ultimately, exiting the eurozone.

Earlier Tuesday, the Greek government submitted a new bailout proposal, just hours away from the expiration of its current rescue deal. Greece is set to default on a payment to the IMF at midnight Tuesday, heightening fears that the country could exit the currency union not long after.

"People are just taking risk off the table," said Rebecca Patterson, chief investment officer at Bessemer Trust in New York. "The trade contagion is limited and the financial contagion is limited."

The latest turns in Greece's debt saga have whipsawed stocks in recent weeks. Still, many Wall Street traders say the country's prospective exit from the currency union shouldn't be a major driver of U.S. stocks in the long run, given Greece's small size and the U.S.'s improving economic growth.

"Ultimately, the strength of the U.S. economy is the market's saving grace," said Jeremy Zirin, head of investment strategy at UBS Wealth Management Americas. Recent economic reports have been mostly upbeat, he said, pointing to May's employment report and a surge in retail sales.

Many investors and traders say developments in Greece are likely to continue to dominate daily trading at least until Sunday, when Greeks are due to vote on the conditions set by the country's creditors for further aid. Greek government officials told The Wall Street Journal that the country would consider canceling the referendum if a good enough deal was on the table.

The eurozone portion of Greece's EUR245 billion rescue deal runs out at midnight, which is also the last moment Athens can make a EUR1.55 billion payment to the IMF without falling into arrears.

Investors will get updates on the U.S. economy later in the week, with the important monthly jobs report due on Thursday. Economists polled by The Wall Street Journal expect 233,000 jobs were added in June, on the heels of 280,000 new positions in May.

Next week marks the unofficial start to the second-quarter earnings season with Alcoa Inc. due to report on Wednesday.

European markets continued to fall Tuesday. Germany's DAX fell 1.25%, while France's CAC-40 fell 1.6%.

Treasury prices edged lower, lifting the yield on the 10-year note to 2.335% from 2.333% on Monday.

In commodity markets, gold futures slipped 0.6% to $1171.50 an ounce. Crude-oil futures added 2% to $59.47 a barrel.

Meanwhile, insurance broker and risk advisory firm Willis Group Holdings and professional services group Towers Watson on Tuesday said they had agreed to an all-stock merger that valued the combined company at $18 billion. Willis Group's shares climbed 3.3%, while Towers Watson fell 8.8%.

Saumya Vaishampayan contributed to this article.

Write to Dan Strumpf at daniel.strumpf@wsj.com