Marquez converted his pole position into a lead at the start, but the reigning champion had no answer to the straightline speed of Jorge Lorenzo's Ducati.
Dovizioso, who was second in Austria to his then team-mate Andrea Iannone a year ago, took advantage of the battle between Marquez and Lorenzo to snatch the lead.
Lorenzo cleared team-mate Dovizioso at Turn 1 and passed Marquez on the next straight, assuming control of the race.
Marquez fended off Dovizioso as Lorenzo increased his advantage, but the defending champion slipped back to third briefly before regaining second spot.
Their battle allowed Pedrosa to close right in around six laps from the end but he couldn't challenge for the victory and Dovizioso prevailed. Dovi qualified 0.144 of a second behind, and Lorenzo 0.386 back.
Balasa Dinverno & Foltz LLC Reduces Stake in Coca-Cola Company (The) (NYSE:KO)
Portfolio Solutions LLC purchased a new stake in shares of Coca-Cola Company (The) during the first quarter worth about $108,000. The shares were purchased at an average price of GBX 2,240 ($29.17) per share, for a total transaction of £2,329.60 ($3,033.73).
The Honda's of Dani Pedrosa and Cal Crutchlow were eighth and ninth, with Suzuki' Andrea Iannone tenth, despite a mechanical problem during FP4.
The pair traded the lead several times on that lap, with Marquez unable to make a move stick and easing the pressure on the Ducati for a few laps afterwards.
Marquez hunted down Dovizioso in thrilling duel and made one last bold move at the final corner, cutting inside the leader but running too wide to enable the Italian to pass him and seal his third win of the year.
Yamaha's Maverick Vinales produced a late recovery to climb up to fourth on the grid, with Ducati's Danilo Petrucci rounding out the top five.