U.S. equities closed higher, reversing earlier weakness following a terror incident near New York City's Times Square.

The Dow Jones industrial average notched a record close, rising 56.87 points to 24,386.03. The S&P 500 gained 0.3 percent to finish at 2,659.99, an all-time closing high. Energy and tech stocks were among the best-performing sectors in the S&P 500, rising 0.7 percent and 0.8 percent, respectively.

"For the first time in this recovery, energy stocks appear significantly undervalued. They currently trade at a 32% discount to fair value," said Jim Paulsen, chief investment strategist at The Leuthold Group, in a note. "Maybe it's time to lighten up on some of your popular 2017 winners and reallocate toward a sector deeply out of favor."

A rise in crude prices also gave energy a boost. U.S. crude futures jumped 1.1 percent to settle at $57.99 per barrel on Monday. Brent futures also rose, hitting a 2½-year high.

The Nasdaq composite gained 0.5 percent, closing at 6,875.08.

The major indexes shook off news of the explosion that took place in Times Square earlier on Monday. A low tech device exploded during rush hour Monday morning at New York's Port Authority, injuring four people, including the man carrying the device. "This was an attempted terrorist attack," Mayor Bill de Blasio said at a news conference at the scene.

Stock futures pared gains after initial reports of the explosion broke. S&P 500 futures briefly turned negative. Treasurys rose, pushing the yield on the 10-year note to 2.38 percent.

"People recognize that these one-offs aren't that important, but it does remind them that there are risks out there," said Maris Ogg, president of Tower Bridge Advisors.

Stocks were coming off a strong session, with the S&P 500 and Dow posting record closing highs after the release of strong employment data. The nonfarm payrolls data for November revealed Friday that the U.S. had added 228,000 jobs last month, beating expectations of 200,000.

Wall Street also looked ahead to the upcoming monetary policy meeting by the Federal Open Market Committee (FOMC), to see if the central bank raises interest rates for the final time this year. The meeting starts Tuesday and the committee is scheduled to announce its decision on Wednesday.

"Markets are pricing in the near-certainty of a rate hike on Wednesday, continuing on the Fed's gradual tightening path," said Jason Pride, director of investment strategy at Glenmede. Pride also noted that "improving economies in the U.S. and elsewhere suggest that monetary policy normalization will happen globally."