CALGARY, Alberta (Reuters) - Canadian energy producer Imperial Oil Ltd (>> Imperial Oil Ltd) reported an unexpected second-quarter loss on Friday due to planned and unplanned maintenance that curbed oil output.

The net loss of C$77 million ($61.42 million), or 9 Canadian cents per share, was smaller than the loss of C$181 million, or 21 Canadian cents, in the same period a year earlier thanks to a pick-up in oil prices and less refinery maintenance.

Analysts had expected earnings of around 30 Canadian cents a share, and Imperial shares were last down 1.9 percent on the Toronto Stock Exchange at C$36.49.

North American oil producers are slowly gaining from a rise in global crude prices as an OPEC-led production cut and a rebound in demand slowly erode a persistent global glut.

Imperial sold bitumen from its Canadian oil sands projects for C$38.22 per barrel in the second quarter compared with C$29.45 per barrel in the same period of 2016.

The company, majority owned by Exxon Mobil Corp (>> Exxon Mobil Corporation), said gross production rose slightly to 331,000 barrels of oil equivalent per day (boepd) in the reported quarter, compared with 329,000 boepd last year.

Output at Imperial's Kearl oil sands mining project was 171,000 barrels per day in the quarter, up from 155,000 bpd in the same three months of 2016 when wildfires in northern Alberta disrupted oil sands production.

Maintenance at Kearl in the second quarter of 2017 reduced production by about 38,000 barrels per day and Imperial said it will be taking steps this year and in 2018 to improve performance at the plant, which has struggled to hit full capacity of around 210,000 bpd.

"Kearl is a high-quality, long-life asset of significant importance to the company," said Imperial chief executive Rich Kruger. "We are addressing gaps in performance by enhancing existing infrastructure while also evaluating additional innovative ideas to improve results."

Imperial also holds a 25 percent stake in the Syncrude oil sands project, which has been running at reduced rates since March following a fire.

The company said its refinery throughput averaged 358,000 bpd in the quarter, up from 246,000 bpd in the same period of 2016.

(Additional reporting by Anirban Paul in Bengaluru; Editing by Shounak Dasgupta and James Dalgleish)

By Nia Williams

Stocks treated in this article : Exxon Mobil Corporation, Imperial Oil Ltd