(Reuters) - Porsche won the Le Mans 24 Hours sportscar race for the second year in a row on Sunday after Toyota had a first victory snatched from their grasp by a last lap power failure.

Japanese driver Kazuki Nakajima had been on course to take the chequered flag when sudden drama unfolded as he reported a problem with five minutes remaining. He then slowed before coming to a halt.

That allowed Porsche to blast past and claim their 18th victory, with the German manufacturer's number two 919 hybrid car shared by Switzerland's Neel Jani, Frenchman Roland Dumas and Germany's Marc Lieb.

"What can I say? First of all I feel sorry for the boys down in the other garage," said Lieb. "They deserved this, they were giving us a hard time as well. This race should have had two winners, it was really unbelievable."

The 84th edition of the race was watched by a crowd of 263,500 spectators.

Toyota would have been only the second Japanese manufacturer to win the greatest prize in sportscar racing, after Mazda in 1991, but instead finished as runners-up for the fifth time.

Nakajima had shared the stricken number five Toyota TS050 hybrid car with Britain's Anthony Davidson and Switzerland's Sebastien Buemi.

"I literally have no words," said Davidson on Twitter, reporting that Nakajima had said he was ready to cry as he crossed the line.

The Toyota pit crew beat him to that as well, the heartbreak evident across the garage.

Instead it was the number six Toyota that took second place for Japan's Kamui Kobayashi, France's Stephane Sarrazin and Britain's Mike Conway.

Audi finished third with their number eight car.

Porsche and stablemates Audi had each entered only two cars instead of three in the main LMP1 category for cost-cutting reasons in the wake of parent company Volkswagen's diesel emissions scandal.

The number one Porsche hit problems shortly before midnight and ceased to be a contender.

Last year's race was won by Germany's Nico Hulkenberg, New Zealander Earl Bamber and Britain's Nick Tandy but none of them were driving in the main LMP1 category this time.

Hulkenberg was competing in Sunday's European Formula One Grand Prix in Baku for Force India.

Porsche, who returned to Le Mans with a works team in 2014 for the first time in 16 years, are the most successful manufacturer.

The race also marked a winning return by Ford in the GTE Pro category.

(Reporting by Alan Baldwin in Baku, editing by Pritha Sarkar)