The Fortuna Honda riding Elias overtook Rossi on the final corner and out-sprinted the Italian to the finish to win by the smallest of margins, with American Kenny Roberts coming in third.
"It is like a dream what I have done here," said an excited Elias.
"There were several nerve wracking moments on the final laps but I held my nerve and shadowed Valentino. I still didn't know whether I had got it on the line."
Rossi on his Yamaha had the consolation of moving to first in the championship for the firtst time this season, passing Repsol Honda rider Nicky Hayden who crashed out in spectacular circumstances on lap five.
Hayden, who failed to finish for the first time this season, was running in third place behind Rossi when Repsol teammate Dani Pedrosa tried an inside overtaking maneuver on a left-hand bend, but slid out and collided with his American counterpart taking both riders out of the race.
Rossi, on 244 points, now holds an eight point lead over Hayden with just one race remaining in the season.
"Yes I realised I was riding for the championship and tried to play the game," said Rossi.
"For sure victory would be better but with Nicky's crash we are now ahead with eight points and will concentrate in Valencia which I think will be a very good last round."
The final MotoGP race of the season is at the Circuito de la Comunitat Valenciana in Spain on October 29.