The fast-spreading Omicron variant is clouding the outlook for oil markets after a rapid recovery in demand pushed prices to their highest levels in years.

Oil marched higher for much of 2021. Demand lifted as economies revved up, while producers in the Middle East and elsewhere kept millions of barrels of crude each day in the ground. Brent-crude prices, the global benchmark, climbed over 50% to $77.78 a barrel.

The rally delivered bumper earnings at companies such as Exxon Mobil Corp. XOM 0.66% and Chevron Corp. CVX -0.07% , making energy the best-performing sector of the S&P 500 for the year. Miners including Glencore PLC and Freeport-McMoRan Inc. FCX 0.26% also enjoyed share-price run-ups driven by advances in commodities from coal to copper.

Drivers are feeling the pinch. Average national prices for regular gasoline have risen to about $3.29 a gallon from $2.25 a gallon a year ago, according to AAA, though they are down from about $3.40 before the emergence of the Omicron variant of Covid-19. Energy contributed to the fastest pace of consumer-price growth in decades this fall, prompting President Biden to release crude from the strategic reserve.

“We’ve learned that demand can come back with a vengeance,” said Francisco Blanch, head of commodities and derivatives research at Bank of America. He thinks Brent prices could reach $120 a barrel in 2022, barring a jump in Covid-19 hospitalizations or a major outbreak in China.

The world is still using less oil than it did on the eve of coronavirus, consuming about 96.2 million barrels a day this year, according to the International Energy Agency. But demand has snapped back faster than production, and the energy adviser figures demand will reach pre-coronavirus levels of over 100 million barrels a day in the third quarter of 2022.

The longer-term path of demand for—and production of—fossil fuels is another unknown. World leaders in November reached a deal that aims to accelerate emission cuts. The IEA’s forecasts for oil demand over the next three decades hinge on the extent to which governments follow through on climate commitments.

Energy traders and analysts say Omicron won’t deliver the kind of shock to oil prices unleashed by the first coronavirus shutdowns, when U.S. crude futures briefly turned negative. One reason is that demand for oil from the petrochemicals industry is offsetting a decline in jet-fuel consumption. Giving oil demand another boost, a surge in natural-gas prices in Europe and Asia encouraged utilities to burn fuel oil and coal to generate electricity.

Investors have recently sold oil to lock in profits, pushing prices lower than justified by the likely effect of Omicron on demand, said Rebecca Babin, senior energy trader at CIBC Private Wealth U.S. “We’ve had a pretty amazing year in energy,” she said.