The S&P 500 had a monster year in 2021, surging more than 27%. Two of the biggest fuel sources driving that rally were strong corporate earnings as the economy started rebounding from the pandemic and favorable monetary policies from central banks. Those catalysts helped propel most stocks higher.

However, some of the biggest gains came from a surprising sector: Oil stocks. Three of the 10 best-performing S&P 500 stocks last year were oil producers. Devon Energy (DVN -1.39%)Marathon Oil (MRO -1.10%), and Diamondback Energy (FANG -0.48%) all delivered gains of more than 100%. Here's a closer look at what drove this surprising performance in the oil patch. 

A person holding 2021 with a chart showing an up arrow.

Image source: Getty Images.

A gusher of dividends

Devon Energy's stock surged an impressive 178% in 2021. Higher oil prices helped fuel that rally -- the main U.S. oil price benchmark, West Texas Intermediate, rallied nearly 60% to over $75 a barrel -- enabling Devon to generate a record $1.1 billion of free cash flow in the third quarter. That was up eightfold from the fourth quarter of 2020. 

However, oil wasn't the only catalyst. An equally important driver for Devon was closing its transformation merger with WPX Energy in early January. That deal created a leading, low-cost oil producer that prioritizes generating free cash flow and returning money to investors. 

Part of that return was the launch of the industry's first fixed-plus variable framework. In addition to making regular fixed quarterly dividend payments, Devon started paying out up to 50% of its excess free cash flow each quarter in variable dividends. Those payments grew along with its cash flow as oil prices rose, and it captured cost savings from the WPX merger. Overall, the company paid out $1.97 per share of regular and variable dividends in 2021, pushing its total return over 197% for the year. On top of that, Devon launched a $1 billion share repurchase program -- enough to retire 4% of its outstanding stock -- giving shares even more fuel to rally. 

Cashing in on higher crude prices

Marathon Oil's stock surged more than 145% in 2021. Likewise, it benefited from higher oil prices, which fueled significant free cash flow. Marathon generated $1.3 billion of free cash flow through the third quarter. 

The oil company used that money to repay debt and reward shareholders. Overall, it reduced debt by $1.4 billion, funded with balance sheet cash and free cash flow. That debt repayment will save it $50 million of annual interest expenses, improving future free cash flow. Meanwhile, Marathon raised its dividend three times during the year and 100% overall. That surging dividend helped push its total return over 150% for the year. 

Marathon also launched a $2.5 billion share repurchase program in October. It bought back $200 million that month and planned to repurchase $500 million by year's end. These buybacks helped give the stock some extra fuel to rally in 2021. 

A big year of wheeling and dealing

Shares of Diamondback Energy were up more than 123% in 2021. Again, higher oil prices helped fuel its rally because they significantly improved its free cash flow. It generated $1.65 billion in excess cash through the third quarter, up 12 times from 2020's level.

In addition, Diamondback got a boost from closing two acquisitions early in the year. It completed its deal for Guidon Operating in February and its QEP Resources merger a month later. The company also sold some noncore assets to repay debt, including QEP Resources' Williston Basin assets. 

These deals and debt repayment set Diamondback Energy to generate more free cash, half of which it will return to shareholders. The company increased its dividend three times in 2021, growing it by 33% for the year. It also accelerated its plans to return additional cash to shareholders by launching a $2 billion share repurchase program. Diamondback's growing dividend helped push its total return to nearly 130% in 2021. 

Cash flow gushers

Devon Energy, Marathon Oil, and Diamondback Energy cashed in on higher crude oil prices in 2021. Those higher prices enabled this trio to generate a gusher of free cash flow, which they used to repay debt and return additional money to shareholders. That combination of surging free cash and growing shareholder returns gave this trio the fuel to deliver some of the best performances in the S&P 500 in 2021.

While investors shouldn't expect them to deliver triple-digit gains again in 2022, they should be able to produce and return another gusher of cash this year if oil prices cooperate. That could give them the fuel to deliver attractive returns again this year.