Casey Stoner produced a remarkable late charge to win in Valencia as MotoGP remembered Marco Simoncelli.
The riders in all three classes gathered for a tribute before the race to Simoncelli - who was killed in the Malaysian MotoGP.
Stoner escaped a first-corner collision which saw four riders crash and somehow overtook Ben Spies on the final corner to win his 10th race of the year.
Simoncelli's Gresini team earlier saw Michele Pirro win the Moto2 race.
British rider Cal Crutchlow managed to finish fourth - the best finish of his MotoGP career - as he claimed the rookie of the year award.
The MotoGP riders started the race on slick tyres in difficult conditions as a light drizzle fell over some of the circuit, and there was instant drama as four riders crashed out at the first corner.
Alvaro Bautista clipped the back tyre of Andrea Dovizioso and slid off, skittling the bikes of Valentino Rossi, Nicky Hayden and Randy De Puniet in the process.
All four riders were unhurt in the accident.
Stoner got away safely to lead from Dani Pedrosa and Spies, before Dovizioso battled through to take second.
Stoner backed off as the rain started to fall heavily and Spies moved up into second place, with the lead falling to 1.294 seconds with four laps remaining.
Spies then passed Stoner as the world champion ran wide, and the American looked certain to win his second race of the season.
But Stoner somehow made up the gap coming out of the final corner to win the final race of the 800cc era by just 0.015 seconds.
Stoner told BBC Sport: "I had a massive lead and was managing that but as soon as more rain came I wasn't willing to push and take risks.
"I made a mistake and went into neutral and Ben came past and rode a good few laps. I didn't know how I could get past him but I realised I could get a much better drive out of the last corner so I tried to get as close as I could.
"It was the last lap of the last race of the year so I thought, 'Let's just go for it' - and I got it. It was one of the hairiest races of my career."
Earlier, team Gresini rider Michele Pirro won an emotional Moto2 race.
Simoncelli's Gresini team were originally not going to compete in Valencia but decided to take part and Pirro duly won the race to leave team boss Fausto Gresini in tears on the pit wall. Stefan Bradl crashed out of the race but had already won the Moto2 championship.
Maverick Vinales won the final ever 125cc race as Nicolas Terol took the championship. The class will be replaced by the new Moto3 championship from next season.
Valencia MotoGP race result:
1) Casey Stoner (Aus) Repsol Honda 48'18.645
2) Ben Spies (USA) Yamaha 48'18.660
3) Andrea Dovizioso (Ita) Repsol Honda 48'24.581
4) Cal Crutchlow (Gbr) Tech 3 Yamaha 48'27.363
5) Dani Pedrosa (Spa) Repsol Honda 48'27.966
6) Katsuyuki Nakasuga (Jpn) Yamaha 48'42.463
7) Josh Hayes (USA) Tech 3 Yamaha 48'51.763
8) Karel Abraham (Cze) Cardion Ducati 48'56.597
9) Loris Capirossi (Ita) Pramac Ducati 49'07.598
10) Toni Elias (Spa) LCR Honda 49'11.146
Moto2 race results:
1) Michele Pirro (Ita) Gresini Moriwaka 46'22.205
2) Mika Kallio (Fin) Suter 46'28.355
3) Dominique Aegerter (Swi) Suter 46'28.568
125cc race results:
1) Maverick Vinales (Spa) Aprilia 41'44.138
2) Nicolas Terol (Spa) Aprilia 41'47.354
3) Hector Faubel (Spa) Aprilia 41'51.598