Canada's main stock index gained on Wednesday as shares in energy companies benefited from a rebound in oil prices, offsetting losses among gold miners and other materials stocks.

Oil prices jumped more than 3 per cent after a surprisingly strong drawdown in gasoline stockpiles, pushing the U.S. benchmark back above $40 a barrel after falling from $50 in late June.

The Toronto Stock Exchange's S&P/TSX composite index ended the session up 35.04 points, or 0.24 per cent, at 14,512.05.

Its energy group, a major component, climbed 2.2 per cent.

"We think it's the shorts that are driving the (oil) market lower," said Philip Petursson, chief investment strategist at Manulife Investments, referring to speculative bets that oil prices will fall. "Equity investors are looking through that to perhaps rising oil prices through the end of this year."

Suncor Energy Inc (SU.TO) gained 1.5 per cent to $34.56 and Canadian Natural Resources Ltd (CNR.TO) rose 1.7 per cent to $39.43.

Husky Energy Inc (HSE.TO) advanced 6.9 per cent to C$16.24. The company said it had reached agreement with China's CNOOC over a gas project.

The materials group, which includes precious and base metals miners and fertilizer companies, lost 1 per cent as the price of bullion fell from a three-week high hit in the previous session.

The most influential weights included Barrick Gold Corp (ABX.TO), down 1.4 per cent to $29.32 and Kinross Gold Corp (K.TO), off 1.9 per cent at $7.09.

The sector has in recent months helped to lessen the impact on the Canadian market of global shocks, including Britain's vote to leave the European Union, as investors back gold miners amid economic uncertainty.

Turquoise Hill Resources Ltd (TRQ.TO) fell 6.6 per cent to $4.25 after announcing quarterly results late on Tuesday.

Six of the index's 10 main groups were in positive territory, with gainers just outnumbering decliners.

Consumer staples rose 0.4 per cent as dairy company Saputo Inc (SAP.TO) jumped 5.3 per cent to $41.84. Several analysts raised their price targets for the company's shares after it reported a profit jump, boosted its dividend, and said it remains interested in acquisitions.

Shares in Shopify Inc (SH.TO) surged 9.3 per cent to $48.01 after the e-commerce software maker reported a smaller-than-expected quarterly loss and provided a more upbeat outlook.

In the U.S., The Dow Jones industrial average .DJI rose 41.23 points, or 0.23 per cent, to 18,355, the S&P 500 .SPX gained 6.75 points, or 0.31 per cent, to 2,163.78 and the Nasdaq Composite.IXIC added 22.01 points, or 0.43 per cent, to 5,159.74.