Frenchman Fabio Quartararo came third, which enabled him to climb two spots in the overall classification to the seventh place.
Marquez headed into the last lap leading the race, but as he tried to secure the win, his opponent took him by surprise in the last turn of the final circuit, Efe news reported.
Speaking after the race, Marquez said: "I'm happy because today we made a mistake with the rear tyre, immediately I saw all the soft tyres had better grip and even their consistency at the end was better than us. Even like this I tried because I'm Marc and I need to try!"
"The second place is good, we missed out in the last corner with a lot of sliding but Dovi did an incredible job. Still we are able to leave here with a 58-point lead so we will keep continuing like this. Even when we are not in the best shape, we are able to finish like this which is positive overall," he added.
The Spaniard tried to extend the domination he has displayed over the weekend from the beginning of the race.
But he once again found himself face-to-face with Dovizioso after both staged a one-on-one competition through several turns.
During the first lap, Quartararo managed to take an advantage of a few metres and Dovizioso came second, while the Repsol Honda rider, who tried to avoid mistakes, was nipping at the heels of Ducati Desmosedici's Jack Miller of Australia.
In the early stages, the race lost two riders as Briton Cal Crutchlow hit the ground and Spaniard Pol Espargaro quit due to technical issues.
During the fourth lap, Marquez overtook Miller to position himself behind Dovizioso, who was closing the gap with Quartararo.
Dovizioso took the lead in the fifth lap with a new lap record.
It was in the next lap that Marquez decided to surpass Quartararo to chase Dovizioso.
Then Marquez went after Dovizioso's position in the eighth lap, overtaking him after a back and forth.
Marquez seemed to be on his way for a maiden win at the Red Bull Ring, but with 19 laps to go, the race was far from finished, especially with Dovizioso not throwing in the towel.
The Spaniard fended off several attempts by Dovizioso, who intensified his efforts in the final three laps. Dovizioso then managed to turn the table in a nail-biting final lap.
( With inputs from IANS )