Marquez started from pole and regrouped from an early mistake to continue the form he has shown all weekend and take his fourth win of the campaign.
He now leads the championship by 52 points over Rossi with four grands prix and a maximum of 100 points remaining.
Maverick Vinales took the lead at the second corner, sweeping through from third as Marquez passed Lorenzo for the first of two times on the opening lap.
Marquez overtook Vinales on the second lap at Turn 15, but his time at the front was fleeting, making a mistake at Turn 7 on the following lap and dropping to fifth.
While Marquez recovered with a string of passes at Turn 15, Rossi moved past Lorenzo and then quickly cut Vinales' margin, which had grown to 0.825 seconds.
In a battle of the 2017 Yamaha team-mates, Rossi grabbed the lead at Turn 4 on lap nine, which triggered the end of Vinales' fight for victory.
The next time around, Vinales overshot the first corner and relinquished second place to the recovering Marquez, and was then passed by Lorenzo at Turn 12.
At the front, Marquez slashed Rossi's six-tenths advantage and then found his way past with another move at Turn 15, with 11 laps remaining.
From there, the race was Marquez's.
He pulled away a couple of tenths at a time, before delivering the key blow on lap 15, turning the fastest lap of the race, a 1m48.694s, nearly a full second faster than what Rossi managed.
Marquez continued his charge at the front, his lead getting out to 3.5s before he backed off on the final lap to finish 2.7s clear.
The victory is Marquez's first since the German Grand Prix in July, and his first on home soil since the 2014 season finale at Valencia.
After Marquez pulled clear, Rossi fell back into the clutches of Lorenzo, who eventually shook off the challenge from Vinales when the Suzuki rider ran wide at Turn 12.
Lorenzo got past Rossi with five laps remaining, but then he could not pull away from the Italian.
With two laps remaining, Rossi had a look down the inside at Turn 12 but had braked too late as he sailed past Lorenzo and wide onto the escape road.
That gave Lorenzo a smooth run to the flag to finish in the top two in a race for the first time since his last win, at Mugello in May, and he moves back to within 14 points of Rossi in the standings.
Rossi was three seconds adrift at the end, with Vinales settling for fourth a further two seconds behind.
Cal Crutchlow fell from fifth to eighth on the first lap, but he fought back to pass Andrea Dovizioso, Aleix Espargaro and finally Dani Pedrosa.
In finishing fifth for LCR Honda, Crutchlow was the top satellite rider, while Pedrosa reclaimed sixth from from Espargaro on the second Suzuki two laps from home.
Tech3 Yamaha's Pol Espargaro was eighth, while the Aprilias of Alvaro Bautista and Stefan Bradl completed the top 10.
Ducati had a tough day. Andrea Dovizioso lined up fourth and was considered the best chance of a ninth different winner in as many races.
He was in the lead pack early, but faded dramatically to eventually finish 11th, one spot ahead of Michele Pirro standing in for the injured Andrea Iannone.
Making a one-off return to deputise for Marc VDS Honda's Jack Miller, 2006 world champion Nicky Hayden grabbed the final point with 15th.
It was a day to forget for Pramac Ducati, with Danilo Petrucci and Scott Redding coming home in 17th and 19th.
Petrucci was given a ride-through penalty for contact with Redding on the opening lap, which sent Redding down.