This win marks Dovizioso's fifth victory of the season, matching Marquez's tally for 2017 - as the championship gap sits at only 11 points with three races remaining in the season.
The title front-runners were joined on the podium by wet-weather specialist Danilo Petrucci, who enjoyed the damp conditions over the weekend in Motegi.
Frenchman Johann Zarco started the race on pole, with Petrucci and Marquez alongside him on the front row - but it was Marquez who got the holeshot off the start, jumping into the lead at the first corner, but a huge surge from the Ducati of Jorge Lorenzo saw him lead after one lap.
However, that lead quickly evaporated on the next lap - when both Petrucci and Marquez moved past Lorenzo, who continued to slip as the laps ticked by, with his Ducati team-mate Dovizioso climbing into a podium position from 9th on the starting grid and passing Lorenzo in the process.
The front three of Petrucci, Marquez and Dovizioso then started to open up a gap on the rest of the field, with Johann Zarco also jumping ahead of Lorenzo to establish himself in the top four.
With 12 laps to go, Marquez finally found his way past Petrucci to lead the race and was closely followed by Dovizioso who moved up into second - setting up a battle between the Championship leaders for the final 10 laps
While Marquez was in the lead, Dovizioso would not relent - and the Italian rider stayed in close pursuit as time ticked down, not allowing his World Championship rival to open up a gap at the top of the standings.
As the riders crossed the starting line with six laps remaining, Dovizioso looked to finally make his push past Marquez and into the league - but the pair played a game of cat and mouse, trading control of the lead a couple of times before Marquez emerged in front again.
With each lap, Marquez retained control - heading into the dramatic final lap, the Spaniard's lead was just over half a second...but a minor mistake from the Honda rider opened the door for Dovizioso to pounce.
With four laps remaining, the Ducati rider pushed his way into the lead - as the two rode neck and neck towards the chequered flag...but it was Dovizioso who was victorious on the day, keeping his championship dreams alive.
Danilo Petrucci was next to finish, 10 second off-pace in third - while Suzuki's Andrea Iannone had his best race of the year by finishing in fourth, with his team-mate Alex Rins close behind in fifth.
Jorge Lorenzo finished in sixth after his hot start, while Aleix Espargaro (Aprilia), Johann Zarco (Monster Yamaha), Maverick Vinales (Movistar Yamaha) and Loris Baz (Avintia) rounded out the Top 10