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.
Yuvraj Singh, Ravichandran Ashwin, Ravindra Jadeja left out for SL ODIs
Named in India's Champions Trophy squad , Pandey returned to India because of an injury that flared up. India's set to play five ODIs and a T20I against Sri Lanka , starting from August 20 in Dambulla.
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.