By Anna Hirtenstein and David Benoit

Energy shares climbed with the price of oil Tuesday, pushing the Dow Jones Industrial Average higher after a fiscal stimulus package from the European Union boosted hopes of mitigating the pandemic's economic fallout.

The blue-chip index gained 159.53 points, or 0.6%, to 26840.40 and the S&P 500 rose 5.46 points, or 0.2%, to 3257.30. The Nasdaq Composite fell 86.73 points, or 0.8%, to 10680.36, a day after a closing at a record.

The energy sector powered gains in the S&P 500, rising 6.2%, while Exxon Mobil added $2.15, or 5.1%, to $44.65 and Chevron climbed $6.12, or 7.2%, to $91.39 to boost the Dow industrials. Chevron on Monday had struck a $5 billion deal to acquire Noble Energy, saying the deal would help it combat the downturn.

The rally carried oil prices to their highest levels since early March, when they plunged amid a pricing war between Saudi Arabia and Russia. U.S. crude rose 2.8% to $41.96 a barrel, its highest close since March 5.

The gains came after the European Union reached a historic pact overnight that signaled greater unification and spending across its members. The bloc agreed on terms for a EUR1.8 trillion ($2.06 trillion) spending plan. The package will center around the bloc's first-ever issuance of common bonds, and could help deepen the bloc's economic integration. The pan-continental Stoxx Europe 600 rose 0.3%, paring earlier gains, to its highest close since early March.

"Having an agreement on fiscal coordination in a time of crisis...probably gives Europe a level of stability it has lacked," said David Kelly, chief global strategist at J.P. Morgan Asset Management. "That really is a big deal."

In the U.S., there are other signs Washington is also moving toward another round of stimulus, though Republicans and Democrats remain split on key issues.

Earnings reports from some of the largest U.S. companies also lifted investors' confidence. Earnings for S&P 500 companies are still expected to register a fall of roughly 44% from a year ago, according to analysts polled by FactSet. A wave of expected reports throughout the week, including from big names such as Amazon.com, Microsoft, and American Express, will shed further light on the economic views of business leaders.

Shares of Coca-Cola rose $1.08, or 2.3%, to $47.20 after the company said revenue fell 28% in the latest quarter as lockdowns to stop the spread of the coronavirus sunk sales of its beverages at restaurants, movie theaters and stadiums. But the company also said the worst was behind it and July was shaping up better.

Lockheed Martin's shares rose $9.59, or 2.6%, to $375.12 after the defense giant reported that its quarterly revenue and profit were above expectations and revised its full-year outlook higher.

IBM also managed a better-than-expected second-quarter profit, as sales fell just 5.4% and it got a big boost from cloud-computing revenue, up 30%. Shares climbed early in the session, but closed down 31 cents, or 0.3%, at $126.06 after a late afternoon slump in tech shares dragged on major indexes.

Gains in tech shares including Amazon.com and Apple have helped carry major indexes higher recently. The S&P 500 tech sector fell 1% Tuesday. Apple lost $5.43, or 1.4%, to $388 and Amazon fell $58.55, or 1.8%, to $3,138.29.

There were other signs investors remain cautious.

Gold prices continued a recent climb, rising 1.5% to $1,842.40 per troy ounce, their highest settle since September 2011. Investors are buying the metal as a haven asset and to hedge against potential inflation as central banks continue to pump money into the system, according to Luc Filip, head of private banking investments at SYZ Private Banking.

"It's almost a no-brainer, let's buy some gold to hedge when there's many uncertainties still," Mr. Filip said. His company currently holds between 4.5% and 5% of its portfolio in gold.

Front-month silver notched a third consecutive session of gains, rising 6.8% to settle at $21.497 per troy ounce, its highest close since March 2014.

In Asia, most major equity benchmarks ended the day with muted gains. Hong Kong's Hang Seng Index was among the biggest gainers after it rallied more than 2%. Japan's Nikkei 225 rose 0.7%.

Write to Anna Hirtenstein at anna.hirtenstein@wsj.com and David Benoit at david.benoit@wsj.com