It's the final day of the second quarter and like recent quarters before it market performance was largely dictated by the ebbs and flows of oil. 

"This is a quarter that was controlled by oil, almost entirely," said Jim Cramer, portfolio manager of the Action Alerts PLUS Charitable Trustin an interview at the New York Stock Exchange on Thursday. "When oil goes up ... the market goes up. When oil goes down, the market goes down. There really isn't much more to it." 

Crude oil prices have rallied over the past three months, buoyed by disruptions to global production. A massive wildfire in Canada's Alberta region during spring sharply reduced the nation's output, while infighting in Nigeria cut off oil production and political and economic upset in Venezuela choked supply. 

In the U.S., a prolonged period of low prices for oil forced major producers to reduce capital expenditures and exploration. Weekly reads on domestic oil inventories have recently shown a fairly consistent decline. 

On Wednesday, U.S. crude enjoyed its best gains in two months, climbing more than 4%, in a rebound from heavy losses following the pro-Brexit vote last week. Another decline in crude inventories data from the Energy Information Administration also boosted oil prices. 

West Texas Intermediate crude oil was on track to close with quarterly gains of more than 20%, even as prices fell on the final day of the three-month period. The commodity topped $50 a barrel in late May for the first time since October 2015.

Since April, the Energy Select Sector SPDR ETF (XLE) - Get Report has rallied nearly 10%. Exxon Mobil (XOM) - Get Report has risen 11.5%, Chevron (CVX) - Get Report has jumped 9.7%, BP (BP) - Get Report has climbed 17.1%, Royal Dutch Shell (RDS.A) has increased 13.3% and Total (TOT) - Get Report has advanced 6.4%.

The S&P 500 is on track for gains of just 1.5% for the three months ending June 30.