Andrea Dovizioso closed the gap on MotoGP points leader Marc Marquez after coming out on top in an epic dice for glory in the wet at Motegi.
The race turned into a two-way fight between this year's title protagonists, a reprise of their pulsating Red Bull Ring duel earlier the year, which was also won by Dovizioso.
Marquez looked to have the edge until suffering a major wobble at Turn 8 on the final lap, allowing Dovizioso to close and make the race-winning pass at Turn 11.
As in Austria, Marquez tried to fight back at the very last corner, but ran wide, allowing Dovizioso to score his fifth victory of the season by 0.249s from the reigning champion.
Danilo Petrucci enjoyed a lengthy spell in the lead early on, and held an advantage as big as 1.9 seconds by the end of the fourth lap with the help of the extra-soft rear tyre.
But the Pramac Ducati rider couldn't keep up the pace, and after being reeled in, the Italian was passed in quick succession by Marquez and Dovizioso on laps 13 and 14 respectively.
With Petrucci now out of the picture, the gap between Marquez and Dovizioso hovered around the half-second mark until Dovizioso made his first attack on lap 19, grabbing the lead at Turn 5.
Dovizioso led by eight tenths at the end of that lap, but Marquez rapidly closed in and took the lead back with three laps to go to set up another thrilling showdown.
The pair traded the lead twice more on the same lap, with Dovizioso making a bold move at Turn 6 only to lose the advantage three turns later, after which Marquez appeared to have the win sealed.
That was until his final-lap error, which allowed Dovizioso to close and pass to take the win, closing his deficit to Marquez in the standings to just 11 points.
Petrucci held on to third place, 10.308s down on Ducati stablemate Dovizioso at the chequered flag.
Suzuki pair Andrea Iannone and Alex Rins delivered the manufaturer its best result of the season in fourth and fifth places, both passing Johann Zarco's Tech 3 Yamaha late on.
Ducati's Jorge Lorenzo, who led the opening lap but soon found himself shuffled back to the lower end of the top 10 after being pushed out wide by poleman Zarco, passed the Frenchman to take sixth, with Aleix Espargaro (Aprilia) following through for seventh.
Zarco ended up eighth ahead of the factory Yamaha of Maverick Vinales. Valentino Rossi crashed out of ninth place on the sixth lap to complete a disappointing day for the marque.
A late problem for Dani Pedrosa's Honda elevated Loris Baz (Avintia Ducati) to the final top 10 place, ahead of Pol Espargaro on the best of the KTMs.
Katsuyuki Nakasuga was the best of the local contingent in 12th on his wildcard Yamaha, as Hiroshi Aoyama (Marc VDS) struggled to 18th and Kohta Nozane (Tech 3 Yamaha) was an early crasher.
Cal Crutchlow's nightmare weekend continued with two crashes in the race on his LCR Honda, while Aspar duo Alvaro Bautista and Karel Abraham were the remaining retirees, both falling.
|3||Danilo Petrucci||Pramac Ducati||Ducati||10.557s|
|8||Johann Zarco||Tech3 Yamaha||Yamaha||29.475s|
|10||Loris Baz||Avintia Ducati||Ducati||48.506s|
|14||Hector Barbera||Avintia Ducati||Ducati||1m03.118s|
|15||Tito Rabat||MVDS Honda||Honda||1m03.514s|
|16||Scott Redding||Pramac Ducati||Ducati||1m04.162s|
|18||Hiroshi Aoyama||MVDS Honda||Honda||1m13.250s|
|-||Alvaro Bautista||Aspar Ducati||Ducati||Retirement|
|-||Karel Abraham||Aspar Ducati||Ducati||Retirement|
|-||Cal Crutchlow||LCR Honda||Honda||Retirement|
|-||Kohta Nozane||Tech3 Yamaha||Yamaha||Retirement|