Pedrosa clocked a time of 46 minutes and 8.125 seconds on his Repsol Honda, just 0.337s ahead of Johann Zarco of France (Monster Yamaha Tech 3), reports Efe.
It wasn't as clear cut as many expected, however, with Marquez so almost crashing out of the race at the mid-way point, losing the front at turn one and miraculously saving it.
It's a fight to the finish today as the Valencia Grand Prix will decide the 2017 MotoGP champion.
Marquez was even able to shrug off his 27th crash of the season after posting his best time of 1min 29.897sec for his eighth pole of the season.
"I have a really good relationship with him, especially also the Ducati guys; I saw on the big screens they go to my box to congratulate the mechanics".
Marquez's title bid was almost derailed six laps from the end when he ran into the first corner too quickly and lost the front under braking, though he miraculously save the incident and safely navigated his way out of the gravel trap.More news: Arab League To Meet On Iran at Saudi Request
Lorenzo seemed to be getting messages from his garage to let Dovizioso pass but refused to do so until he went off.
Zarco surpassed Pedrosa in the second lap, chasing Marquez, who conceded the lead in the fourth lap, as the latter was focusing on the world title rather than the race. Unfortunately for the Spaniard it was slightly too hard, as he lost the front on the brakes.
Marquez has now moved past Jorge Lorenzo, a three-time MotoGP champion who joined Dovizioso on the DNF list in Valencia, to become the most successful Spanish rider of all-time.
Up front Pedrosa and Zarco were left to battle it out for the race win in a pulsating final few laps.
Jack Miller crossed the line seventh ahead of close friend and fellow Honda rider Cal Crutchlow while Michele Pirro and Tito Rabat rounded out the top ten. Bradley Smith (Red Bull KTM Factory Racing) +30.835s 12.
The result sees Marquez take the title from Dovizoso by 37 points with Vinales miraculously third after a tough end to the year.