U.S. stock indexes climbed and government-bond yields fell after inflation data showed that consumer prices rose at the fastest annual pace in four decades in March, largely driven by a surge in energy prices.

The S&P 500 rose 1%, the Dow Jones Industrial Average edged up 0.8% and the technology-heavy Nasdaq Composite rose 1.4%. The gains marked a course reversal from Monday when major indexes retreated, with the benchmark S&P 500 losing 1.7%.

The yield on the benchmark 10-year Treasury note fell to 2.689% Tuesday, having risen earlier to its highest level since 2018. On Monday, the yield settled at 2.779%. Yields fall when bond prices rise.

Labor Department data showed that the consumer-price index rose 8.5% in March from a year before. That outpaced February’s 7.9% reading and was higher than some economists had forecast. An especially large share of index’s gains traced to energy prices, which climbed 11% in March from a month earlier.

Another inflation measure showed some moderation. Stripping out volatile food and energy prices, the core price index rose 0.3% in March month over month, below economists’ expectations and down from 0.5% in February. Some investors and analysts attributed bullish moves in stocks and bonds on Tuesday to a softer-than-expected rise in core prices.

There was “a lack of significant upside surprise in the data,” said Craig Erlam, senior market analyst at Oanda. “Maybe that’s given the market some impression that [inflation]…may be plateauing. I think what frightened traders so much in recent months is that they were constantly underestimating it.” 

Money managers expect the reading will factor heavily into the Federal Reserve’s rate-rise decision at its May meeting, and many anticipate the central bank might increase rates by a half-percentage point. Federal-funds futures—derivatives used to bet on the path of interest rates—show a nearly 90% probability of such a rate rise, up from about 78% last week, according to CME Group.

Investors are also anxious to get a glimpse at how corporate profits are faring amid rising prices. Later this week, Delta Air Lines, Bed Bath & Beyond, and a handful of big investment banks and financial firms will be among the first public companies to report their results for the first stretch of 2022.

“So far, we haven’t seen any meaningful demand destruction” from rising prices, said Amanda Agati,

chief investment officer at PNC Asset Management. “I think what will be very telling—and we’re right on the eve of first-quarter earnings season—is what will happen with profitability and margins.”

The answer could help set the path for a fragile stock market, which has been battered this year by investor anxieties around the war in Ukraine and central-bank policy. Strict Covid-19 lockdowns in Shanghai have added to traders’ worries lately, with data showing that China’s restrictions are beginning to weigh on its economy

Investors are also grappling with concerns about the risk of a U.S. recession within the next 12 months.

“I think some people are hoping that today’s [inflation] print will be the high-water mark,” said Huw Roberts, head of analytics at Quant Insight. “The main debate in the markets at the moment is price inflation and growth deflation. At what point does the inflation spike cause the Fed to hike so aggressively…that we tip into a growth deflationary environment.” 

A trader at the New York Stock Exchange on Monday.

Photo: justin lane/Shutterstock

Meanwhile, Brent crude, the international oil benchmark, rose 6.8% to $105.18 a barrel, regaining ground after losses on Monday. Lockdowns in China and planned releases from strategic reserves in the U.S. and elsewhere have pressured oil prices downward recently. The Biden administration plans to temporarily allow high-ethanol content gasoline to be sold this summer, an attempt to ease prices at the pump. 

Shares of energy companies rose alongside oil prices. The S&P 500’s energy sector was its best-performing group in early trading, rising 2.9%. Midmorning, all 11 S&P 500 sectors were in the green.

In Europe, the pan-continental Stoxx Europe 600 fell 0.5%. Deutsche Bank and Commerzbank led losses for the index, with both falling more than 8%. The moves came after an undisclosed shareholder unloaded roughly 5% stakes in both German banks. The share-price drops impacted other banks in the region, with Italy’s UniCredit

down 3.8%.

In Asia, Hong Kong’s Hang Seng added 0.5%, while in mainland China, the Shanghai Composite jumped 1.5%. Japan’s Nikkei 225, in contrast, declined 1.8%.

Write to Caitlin McCabe at caitlin.mccabe@wsj.com and Matt Grossman at matt.grossman@wsj.com