U.S. stocks closed higher Monday as rising optimism that OPEC would reach a deal to cut production—a move seen as necessary for addressing oversupply—delivered a jolt to energy shares.

The day’s gains were broad with all of the S&P 500’s sectors trading in positive territory.

The Dow Jones Industrial Average DJIA, +0.51% rose 88.55 points, or 0.5% to close at 18,329.04. A 2% surge in shares of Exxon Mobil Corp. XOM, -2.47% , and 1.8% gain in Apple Inc. AAPL, +1.57% and Merck & Co. MRK, -0.22% led blue-chip advancers. Wal-Mart Stores Inc., declined 1.1% to weigh on the Dow.

The S&P 500 index SPX, +1.05% closed up 9.92 points, or 0.5%, at 2,163.66. The S&P energy sector rose 1.5% after Saudi Arabia’s energy minister, Khalid al-Falih, said he was optimistic that major oil producers could agree to cut production before year-end. He also said it wasn’t “unthinkable” for crude to rise to $60 a barrel this year, though others said oil bulls had gotten ahead of themselves. His comments and those from Russian Vladimir Putin affirming his intention to freeze, or even cut, crude output helped boost the broader market.

Read:S&P 500’s ‘triangle’ chart pattern warns of a big selloff, analyst says

The Nasdaq Composite Index COMP, +1.71% gained 36.27 points, or 0.7%, to finish at 5,328.67. The Nasdaq-100 index NDX, +1.87% finished up 0.6% at a closing high of 4,893.77.

Oil futures CLX26, rose 3.1% to settle at $51.35 a barrel. A closely watched meeting of oil producers in Istanbul this week follows a convention of members of the Organization of the Petroleum Exporting Countries and non-OPEC countries in Algiers, where a tentative deal to cap production was struck.

“Oil is definitely front and center amid what seems to be a shift in the market,” said Wayne Kaufman, chief market analyst at Phoenix Financial Services in New York. If countries do come together, “that’s a big change, and one that would help our energy companies, which have been in a bad place, profit-wise. That’s good for the market.”

Investors also looked ahead to the coming earnings season, which will provide clarity into how U.S. companies are faring amid tepid international growth and ahead of an expected interest-rate increase by the Federal Reserve. Companies in the S&P 500 are expected to extend their earnings recession to a sixth-straight quarter, according to FactSet data, though sales are predicted to break a six-quarter streak of declines.

While investors and analysts have long expected another quarter of contracting profits, results that miss a lowered bar of expectations could hit a market that continues to trade near record levels.

Earnings season unofficially begins Tuesday afternoon, when Alcoa Inc. AA, -0.73% reports. The giant aluminum maker is presenting its last quarterly earnings report as a single company, as it plans to split in two on Nov. 1.

“If earnings don’t go up, the market will be relying on multiple expansion at a time when interest rates are going higher,” Kaufman said. “That’s not going to work. If we’re going to see stocks go up, then we need to see the earnings picture improve dramatically.”

Economic news: There were no top-tier U.S. economic releases due to hit on Monday, as government workers got a holiday for Columbus Day.

In Federal Reserve news, Chicago Fed President Charles Evans is scheduled to deliver a speech in Australia at about 9:30 p.m. Eastern Time.

Corporate movers: In corporate news, Mylan NV MYL, +0.27% closed up 8.2% following news of a settlement in the company’s EpiPen pricing controversy. The stock was the biggest percentage gainer among S&P 500 components.

On the downside, Twitter Inc. TWTR, +7.09% sank nearly 12% after a weekend Bloomberg report suggested the social media company won’t attract any takeover bids. Meanwhile, shares of Salesforce.com Inc. CRM, +1.11% , once considered a Twitter suitor, rose 6%.

Bristol-Myers Squibb Co. BMY, +1.71% shares dropped 10% after competitor Merck & Co. MRK, -0.22% announced positive results for a rival lung-cancer drug. Merck shares closed up 1.8%.

Dover Corp. DOV, +0.10% dropped 7.7% after the maker of gear for the energy, engineered systems and refrigeration industries cut its profit and sales outlook for the year.

U.S.-listed shares of Royal Bank of Scotland PLC US:RBS UK:RBS fell 2.2% in the wake of reports showing the bank pushed businesses into default.

In deal news, Noble Group Ltd. NBL, -0.66% said it signed a $1 billion deal with Calpine Corp. US:CPN to sell its U.S. energy unit to the power generation giant. Shares of Noble rose 2%.

Other markets:Gold futures US:GCZ6 rose 0.7% to settle at $1,260.40 an ounce. European stocks SXXP, +0.87% closed higher, while Asian markets mostly gained, though both Japan and Hong Kong were closed for a holiday. A key dollar index DXY, +0.12% rose 0.3%.

The Mexican peso MXNUSD, -0.72% advanced against the dollar Monday, with analysts saying the move signaled a belief that Donald Trump’s chances of becoming president had plunged. That follows a tumultuous weekend during which news of a 2005 video in which Trump boasted of groping women and his sexual conquests, drove some Republicans to disavow him.

The second presidential debate between Trump and Democratic nominee Hillary Clinton, which occurred Sunday night, wasn't seen as likely to change the odds of the election results.

Did Trump 'stop the bleeding' with his debate performance?

Check out: MarketWatch’s Economic Calendar

—Victor Reklaitis in London contributed to this article.