This past year was an interesting one for the energy sector. Oil prices went on a wild ride, tumbling back into the low $40s this summer before roaring back into the upper $50s as the year drew to a close, with crude rising more than 7% on the year by mid-December. That rebound should have provided oil stocks with plenty of fuel to head higher this year. However, that hasn't proven to be the case since most declined in value.

That said, not all energy stocks went in reverse this year. In fact, several fueled significant gains for their investors, with the four best-performers on the following chart:

Energy Stock

Year-to-Date Return

Marathon Petroleum (MPC 0.70%)

39.9%

Valero Energy (VLO 0.86%)

29.8%

Andeavor (ANDV)

27.9%

Cabot Oil & Gas (CTRA 1.95%)

16.4%

Energy Select Sector SPDR ETF (XLE 1.20%)

-7.9%

Data source: YCharts. Returns through December 18, 2017.

Here's what fueled those impressive gains in 2017.

Storage tanks with a refinery in the distance.

Image source: Getty Images.

A bounce-back year and a big cash infusion on the way

Several headwinds pushed down profitability in the refining industry in 2016, including at Marathon Petroleum (MPC 0.70%), where income from operations slumped nearly 50% from 2015 to $2.4 billion. However, many of those headwinds stopped blowing this year. That helped fuel a more than 55% improvement in Marathon's income from operations through the third quarter, with it already topping last year's result at $2.8 billion.

While the earnings improvement helped drive Marathon Petroleum's outperformance this year, it wasn't the only catalyst. The other driver was the company's strategic repositioning plan, which included dropping down its remaining midstream assets to its MLP MPLX (MPLX 1.17%) and simplifying their relationship. So far this year, Marathon sold more than $3 billion in assets to MPLX and should drop down the remaining $8.1 billion by early next year. In addition to that, Marathon and MPLX agreed on a $10.1 billion simplification transaction that will eliminate the incentive distribution rights (IDRs) owned by Marathon in exchange for more units of its MLP. These deals will provide Marathon with a boatload of cash as well as a steady stream of future cash flow from its increased ownership in MPLX, which should enable the company to send even more money to investors in the coming years.

Rebounding profits and share buybacks are driving returns

Fellow refiner Valero Energy has also enjoyed a bounce-back year. After plunging 63% in 2016 to $1.7 billion, Valero's adjusted net income net income has risen nearly 25% through the third quarter, already matching its full-year total from 2016 at $1.7 billion. That earnings rebound provided Valero with more cash to return to investors, with it sending them an industry-leading $2.5 billion over the past year. It spent a significant portion of that money on stock buybacks, which has driven its share count lower by more than 2% this year, providing an additional boost for the stock. 

Dual fuels drove this year's rebounds

Andeavor has also benefited from the rebound in the refining industry this year. This has been a transformational year for the company, which began 2017 as Tesoro Corporation. The first step of that transformation came in June, when Tesoro closed its acquisition of Western Refining. The newly combined entity then changed its name to Andeavor and recently capped off the remake by merging its two MLPs into Andeavor Logistics (ANDX) and then subsequently buying out the IDRs to further simplify its ownership of that entity going forward. This string of strategic initiatives positions Andeavor to grow earnings and increase the cash it returns to investors in the coming years.

A closeup of values on a pipeline.

Image source: Getty Images.

A gusher of free cash flow in 2017

Cabot Oil & Gas is the only non-refining company on this list after it just nosed past refining and logistics giant Philips 66 as the fourth best-performing energy stock this year. Several factors drove shares of the natural gas producer higher this year. First, production is up 12% through the third quarter, which, along with lower expenses, helped return the company to profitability. Meanwhile, it generated $125.8 million in free cash flow so far this year, which is nearly $100 million above last year's level. What's so impressive about this performance is that Cabot has delivered improved earnings and cash flow even though the price of natural gas has fallen about 24% since the start of the year. Cabot was able to overcome those lower prices because it has the lowest production costs in the industry, which enables the company to make more money on its production than rivals. 

It was a good year to own a refining stock

Most of this year's best-performing energy stocks were refining companies, which rebounded along with their profits. Refiners tend to do well in years that oil companies don't because they benefit from lower oil prices, which was the case earlier in the year, when crude dropped. That's why it's often a good idea to own a refining stock in a portfolio that also has oil producers because it can help smooth out returns.