The Dow Jones industrial average also closed higher as Cisco Systems' stock popped on the back of a massive cyberattack.

The Nasdaq, meanwhile, was led higher by a rise in large-cap tech stocks, including Facebook and Alphabet. Shares of tech giant Apple also hit record highs.

That said, an increase production from the U.S. could cap energy's gains, said Jason Pride, director of investment strategy at Glenmede. "Our Take: Oil should trade in the $50-70 range as increased demand is met by supply from U.S. share oil producers," Pride said in a note Monday.

"This is a snap-back rally in crude. The rubber band had stretched too long below $55 per barrel; now we're around $50," said Joe Sowin, head of global equity trading at Highland Capital Management.

West Texas Intermediate futures spiked 2.11 percent to settle at $48.85 per barrel after the Russian and Saudi energy ministers said the two countries agreed to extend a production cut until March 2018.

The S&P climbed about 0.5 percent, with materials leading advancers. Energy was also among the best performers after a sharp rise in crude prices.

The S&P 500 and Nasdaq composite closed at record highs on Monday after a rise in tech and oil prices.

Traders work on the floor of the New York Stock Exchange.

A "ransomware" virus dubbed WannaCry hit 200,000 in more than 100 countries on Friday. The virus locked up computers in car factories, hospitals and schools.

President Donald Trump ordered Homeland Security Adviser Tom Bossert to hold an emergency meeting Friday night to assess the threat posed by the attack, Reuters reported.

The PureFunds ISE Cyber Security ETF (HACK) rose more than 3 percent, putting it on track for its best day of the year.

The Dow also received a boost from Johnson & Johnson, which rose 2.71 percent after JPMorgan upgraded the stock.

Equities have traded near record levels recently as investors have shrugged off conflict in Washington that could derail the implementation of pro-growth policies as well as geopolitical tensions.

"Investors have been desensitized with all the news about North Korea and out of Washington," said Christian Magoon, CEO of Amplify ETFs. "I think it;s doing them a disservice."

North Korea said this weekend it tested a new type of missile that can carry a nuclear warhead. However, the U.S. military's Pacific Command said on Sunday the type of missile that was fired was "not consistent" with an ICBM and South Korea's military played down the North's claim of technical progress on atmospheric re-entry.

"Everything seems to slide off this market," said Bruce McCain, chief investment strategist at Key Private Bank. "Other than a potentially cataclysmic event in Washington, ... I think the slowdown in economic data" could threaten the market in the next 3-to-6 months. "In the meantime, the market continues to hold up with its Teflon curtain."

Jeffrey Saut, chief investment strategist at Raymond James, said in a note that he expects stocks to break into new records later this week, citing his models. "While our models are not always right, they are right more times than they are wrong and hereto we are tilting portfolios accordingly."

In economic news, the National Association of Home Builders survey showed sentiment among home builders came in at 70 for May. Anything above 50 is considered positive sentiment. The index was at 58 in May of last year.

Meanwhile, the Empire State manufacturing index dropped to negative 1 in May from a print of 5.2 last month.