Ferrari won the centenary edition of the 24 Hours of Le Mans, with Italian Alessandro Pier Guidi driving the last stint to the chequered flag, for the marque's first overall victory in 58 years.
The success after 342 laps of the Sarthe circuit in north-west France ended a run of five straight triumphs for Toyota in the world's oldest active endurance race.
The number 51 Ferrari 499P shared by Pier Guidi, compatriot Antonio Giovinazzi and Britain's James Calado, beat the number eight Toyota driven by Sebastien Buemi of Switzerland, New Zealander Brendon Hartley and Japan's Ryo Hirakawa by one minute and 21 seconds.
"It's emotional. Unbelievable. I have no words," former F1 driver Giovinazzi told Eurosport television.
"It's a great achievement and after so long, I think this one will go down in history for sure," said Calado.
The number two Cadillac driven by New Zealand's two times winner Earl Bamber and Britons Alex Lynn and Richard Westbrook finished third but a lap down in the 91st edition of the race since it was first run in 1923.
Scott Dixon in the other Cadillac was fourth.
It was Ferrari's first overall victory at the Sarthe circuit since 1965, when American Masten Gregory and Austrian Formula One driver Jochen Rindt won, and 10th in total for the luxury sportscar brand.
Ferrari also have 29 class wins to their credit.
The victory was nail-biting to the end, however, after a final pitstop drama when Pier Guidi came in to fuel to the finish.
The Italian failed to get going from the final stop, the Ferrari stuck as Toyota mechanics watched on, wondering if a stunning victory might after all be snatched from impending defeat.
Pier Guidi started after a reset that seemed to take an age but the AF Corse team's lead was slashed to 51 seconds before the Toyota's final pitstop.
"We gave it all. We were the underdogs today," said Hartley. "Most of the race we were one of the slower cars. It came to us a bit at the end with the hotter track temp.
"The last few stints were probably the best stints I've done. I just did qualifying lap after qualifying lap. I knew if we just tried to put them under some kind of pressure...we did everything, we threw everything at them."
Hirakawa, who was told to go maximum attack for the last two stints, spun against the barriers at Arnage in the penultimate hour and had to pit for repairs to front and rear.
Ferrari had started with the 50 and 51 cars on the front row, a first in 50 years for the marque that waged epic battles with Ford in the 1960s before leaving in 1973 and then returning this year to the Hypercar category.
Ferrari Formula One team boss Fred Vasseur and driver Charles Leclerc, neither of whom have had much to celebrate this season, joined the celebrations after the Italian marque's biggest success of the year so far.
"It feels absolutely amazing, especially having a Ferrari winning after so many years," said Leclerc.
Ferrari and Toyota fought through the night, with only 14 seconds separating the cars with three hours to go.
Toyota's other car, the number seven, retired after a collision with driver/principal Kamui Kobayashi at the wheel with 103 laps gone.
"I had both tyres punctured on the rear and the left rear driveshaft was broken so I had no drive to return," the Japanese driver said after getting out.
Ferrari's 50 car, starting on pole thanks to Italian Antonio Fuoco, finished fifth after losing time to repairs during the night.
The NASCAR Chevrolet Camaro stock car run by Hendrick Motorsports with seven-times Cup champion Jimmie Johnson, former 24 Hours of Le Mans winner Mike Rockenfeller and 2009 F1 champion Jenson Button finished 39th of 62 starters.
The car was competing as a "Garage 56" entry highlighting future technology.
Hollywood actor Michael Fassbender slammed his Porsche 911 into the tyre barrier in the Porsche curves and retired after nearly 20 hours.