Watched by a crowd of 110,000 at the Circuit Ricardo Tormo, the race pitted the 24-year-old Repsol Honda rider against Ducati's Andrea Dovizioso.
Marquez finished in third place, while Dovizioso retired after crashing into the gravel late into the race, ensuring Marquez, the defending world champion, retained his crown.
It was a tense finale at the end of a close-fought 2017 MotoGP season.
Marquez was defending a 21-point championship lead over Dovizioso heading in the 18th and final race and sped into an early lead from pole position.
The Catalan rider sensibly let combative Frenchman Johann Zarco through to head the race. Behind them, Marquez's Honda teammate Dani Pedrosa and the two Ducatis of Jorge Lorenzo and Dovizioso gave chase.
For long periods, it appeared that Lorenzo was inexplicably holding up his teammate. Signals from his crew seemed to be imploring the Malaga man to allow Dovizioso through, but to no avail.
Meanwhile, Marquez followed Zarco at the front, apparently content to let the Frenchman pursue a maiden premier class win. However, as the laps ticked down, Marquez ran out of patience and swept by, only to lose the front end of his Honda in the following corner.
It was the kind of moment that has come to define his career.
As his bike began to slide from beneath him, Marquez jammed an elbow into the tarmac, lifting the Honda back onto its wheels. The bike hit the gravel, but Marquez somehow managed to maintain control and re-join the race, in fifth place.
Shortly after though, Lorenzo and Dovizioso's races ended. Lorenzo spectacularly crashing out, while Dovizioso -- like Marquez -- succumbed to the lure of the gravel. Unlike his rival though, he was unable to keep his bike upright.
Dani Pedrosa denied Zarco his first MotoGP win, with last year's Moto2 champion finishing second. Marquez clawed his way back to third, meaning he could celebrate his championship win from the podium.
Marquez's victory was his fourth in five years in motorcycling's premier class and his sixth world title overall -- he won the 125cc class in 2010 and the Moto2 crown two years later.