Fossil fuels are supposed to be out of fashion. In a world that is pushing for a greener future, even some of the biggest oil producers such as BP are aspiring to move beyond petroleum.

In the US, voters just elected the candidate who promised to “transition away from the oil industry” and unleash a clean-energy revolution — and not the one who openly trumpeted his love of fossil fuels. Every third email I receive is about a new green energy plan or another group pledging to ditch oil and gas equities.

And yet there has been a bull run in US energy equities in the past three months. US oil and gas stocks have far outperformed the S&P 500’s other 10 sectors, gaining more than a quarter since mid-September.

Some investors even now talk of oil’s “last hurrah” — a final big rally over the next few years before the sector enters its inevitable twilight.

One reason is that the outlook for crude oil is rosier than many people expected earlier this year, when energy demand plunged as the pandemic spread. A peak in oil demand is still coming, insist most forecasters. However, maybe not quite yet.

This bullish thesis is not just about rich westerners hopping off on holiday again once they are vaccinated. Stimulus packages are emboldening oil-market bulls, who expect increased government spending to drag up the whole commodity sector, including energy. A weakening dollar is also pushing dollar-denominated oil prices higher.

Goldman Sachs forecasts a V-shaped recovery in oil demand from this year’s low, with the world’s thirst for crude expected to exceed pre-pandemic levels in 2022.

Jeff Currie, Goldman’s global head of commodities research, says the energy transition will itself increase demand for commodities, from copper to crude oil, as new wires are laid and new infrastructure built. He calls it the “revenge of the old economy”.

Investors have also been encouraged by a shift in corporate behaviour since the plunge in energy demand this year. That was particularly brutal for shale producers in the US, the source of most global oil output growth in recent years. US production is down by about 15 per cent compared with record highs struck earlier this year.

The crash has been terrible for some investors, erasing billions of dollars of capital. It was especially painful for more than 100,000 oil and gas workers who lost their jobs in recent months.

But it has also created a more investable shale business, by winnowing out weaker companies. The stronger survivors all say they will eschew the profligate habits for which they became notorious. They pledge to spend well within cash flow and return the rest to shareholders while pursuing only modest supply growth.

This is quite a change for a sector where executives once measured success by the size of their bonus and how quickly they increased oil output (the two were usually linked).

After the pummelling suffered by the energy sector’s equities in the past three years — down about 40 per cent — the stocks suddenly look cheap to the bulls.

“The sector has been beaten up for so long,” says Andrew Gillick, a managing director at consultancy Enverus. “But now management teams have realised they need to show discipline with capital and return cash to shareholders. That’s a big shift — and the market likes it.”

No one should get carried away. Oil and gas stocks now represent a tiny 2.5 per cent of the S&P 500 these days, and the recent price rises are from a low base after years of investors fleeing the sector.

New shale investors, conscious of the long-term trend away from oil, will punish any sign of operators increasing supply the market may not need, says Matt Portillo, an analyst at Tudor, Pickering, Holt & Company, an investment bank. This is partly because investors are less likely to be seeking future earnings potential and more likely to be hunting undervalued stocks.

“Growth investors no longer exist in the sector because there’s terminal demand risk in the future, and value investors are ruling the day,” he says. “The big theme over the next 12 to 24 months will be the massive amount of debt paid down with free cash flow . . . and the massive acceleration in dividend yields.”

One sign of the shift in sentiment towards the sector is the fate of ExxonMobil, a company where activist investors are agitating for the board to ditch a strategy focused on producing more oil.

In early October NextEra Energy, a Florida-based clean power producer, overtook the supermajor to become the US’s largest energy company by market valuation. It seemed a symbolic moment — but this was shortlived. Exxon's shares have risen by a third since then, and the market now judges it to be worth $40bn more than NextEra.