Yamaha rider Spies looked poised for his second win of the year after passing Stoner for the lead in difficult rainy conditions late on, but lost time coming out of the final turn to allow the Australian through for the win by just 0.015 seconds.
The triumph was Stoner's 10th of the season, and 33rd of his career in his 100th MotoGP start, and provided a much-needed boost to the sport following the tragic death of Marco Simoncelli at the last round in Malaysia.
Third place went to Stoner's Repsol Honda team-mate Andrea Dovizioso - securing the Italian third place in the championship - while Britain's Cal Crutchlow enjoyed his best result of the year for Tech 3 Yamaha as he came home in fourth.
Despite being denied victory late on, Spies was sanguine at the chequered flag.
"I'm happy enough with it," the American told BBC2.
"We made a huge charge at the end. Once we got ahead it was hard to understand how slick the track was because nobody was in front of us.
"We tried to just be careful on the last lap as it had started to rain quite heavily, and Casey took a couple more risks in the last couple of corners and had a bit more motor than me down the straight."
The race was the first since the death of Simoncelli in a crash in Malaysia a fortnight ago, and on Sunday morning riders from the MotoGP, Moto2 and 125cc categories united to perform a lap of honour to remember the late Italian.
The sombre mood in the paddock was matched by leaden skies overhead, and as the race started it did so with drizzle in the air and the track damp in places.
After the distressing scenes at Sepang the last thing the sport needed were any further crashes on Sunday - but that is exactly what happened into the first turn as Alvaro Bautista lost control under braking.
The Rizla Suzuki rider came unstuck after tagging the rear wheel of Dovizioso as the pack closed up. Bautista then slewed into Ducati pair Valentino Rossi and Nicky Hayden, and the Pramac Ducati of Randy de Puniet, eliminating all four on the spot.
Mercifully all the riders were uninjured in the crash, which threw the battle for the midfield positions wide open.
At the front, the race quickly developed into a Honda procession, with Stoner establishing a useful gap to team-mates Dovizioso and Dani Pedrosa.
Spies was the Honda trio's only challenger in the early laps, the American keeping Dovizioso and Pedrosa more than honest.
Dovizioso and Pedrosa were fighting for third place in the championship standings, so their battle for second had plenty riding on it, but the pair had no response to Spies when the rain arrived in earnest with seven laps remaining.
Spies dispatched the pair on successive laps before setting off in pursuit of Stoner, who led by 10 seconds before the heavens opened but quickly saw that gap whittle down to just a few bike lengths.
The double world champion stabilised the gap as the rain relented, but with three laps to go he ran wide, allowing Spies into the lead.
Spies appeared fully in control until the very final turn, but was unable to get the power down as he accelerated for the line and Stoner needed no second invitation to come through for a stunning win.
Further back, Pedrosa came home in fifth, while Katsuyuki Nakasuga - deputising for injured title runner-up Jorge Lorenzo at Yamaha - took a creditable sixth.
Another understudy in American Superbike champion Josh Hayes - in for the injured Colin Edwards at Tech 3 Yamaha - took seventh ahead of Karel Abraham (Cardion AB Ducati), while Italian veteran Loris Capirossi (Pramac) was ninth on his final MotoGP outing.