It was Alonso who had led in the closing stages, the Spaniard having worked his way to the front after some clever strategy from Ferrari and the gamble to go for Pirelli's soft tyres on the 38th lap - with Alonso producing a sensational drive to make them last to the end.
Hamilton, meanwhile, had started from the pit lane, survived a spin and brush with the barriers when he was caught out by cold brakes on the opening lap at Turn 2, and was still lurking toward the back of the field - while team mate and polesitter Nico Rosberg led comfortably - when the race turned on lap eight.
The big shake-up was the result of Marcus Ericsson spinning his Caterham heavily into the tyre wall exiting Turn 3. The four leaders - Rosberg, Williams' Valtteri Bottas, Red Bull's Sebastian Vettel and Alonso - were in the final corner and were thus unable to pit as the safety car was deployed, but virtually everyone else did, which changed the order massively.
By the time the lead quartet were able to pit, Ricciardo had emerged in the lead for Red Bull, with Jenson Button in second. McLaren, however, were alone in having gambled on more rain, and while Button - still on the intermediates when the rest of the field had switched to slicks - was therefore able to pass Ricciardo and lead after the restart, his tyres quickly faded and he had to pit again to follow the rest of the field onto slicks. That left Ricciardo back in the lead ahead of Williams' Felipe Massa and Alonso, who'd survived an off-track moment at Turn 1 on the second lap.
At this stage Jean-Eric Vergne was a terrific fifth for Toro Rosso, holding off Rosberg, Vettel and Hamilton, the latter having made up places hand over fist during the confusion.
After a long first safety-car period - extended after Romain Grosjean crashed his Lotus exiting Turn 3 during the cautionary period - the second followed shortly after when Sergio Perez crashed his Force India into the pit wall after riding the damp kerb on the exit to the final corner on the 23rd lap.
Ricciardo immediately pitted, which left Alonso in the lead while Vergne continued to hold back Rosberg, Vettel and Hamilton. Alonso's last stop on lap 38 put Hamilton into the lead for a lap before the Briton stopped for a set of medium tyres - intent on going through to the end - which allowed Ricciardo to hit the front once more.
The order changed again, however, when the Australian stopped on lap 54 to change from one set of softs to another. That left Alonso out front and Hamilton second, the Briton quickly closed down by Rosberg, who was on a differing strategy and still had to pit again.
Mercedes therefore asked Hamilton to let Rosberg through, but the Briton declined. Rosberg did stop again, on lap 56, which dropped him down to seventh, more than 20 seconds off the lead fight - a gap he would set about rapidly carving into.
Try as he might Hamilton couldn't quite pass Alonso, and soon he had Ricciardo on much fresher soft rubber breathing down his neck. The Australian made a couple of attempts before finally overtaking Hamilton round the outside of Turn 2, just four laps from the finish. One lap later and he dived down the inside of Alonso at the first corner, and from then on it was plain sailing as the he replayed his success in Canada and scored the second Grand Prix victory of his career.
Hamilton never did find a way past Alonso, but had to change from attack to defence in the final laps as Rosberg closed right at the end. The German attacked at Turn 2 on the final lap, Hamilton having lost momentum while braking behind Alonso at the first corner, but the Briton pushed his team mate very wide and held on, with the trio finishing 1.1s apart.
Rosberg now has 202 points to Hamilton's 191, after the latter's extraordinary afternoon of damage limitation.
Massa fended off a hungry Kimi Raikkonen for the second half of the race to take fifth, but team mate Bottas had to pit for fresh rubber on the 59th lap and dropped down to eighth, unable to find a way past Vettel.
The world champion was in the middle of the lead fight until he also spun exiting the final turn, tagging the wall but somehow escaping serious damage. The German's gamble to try and get to the finish without a final stop paid off: he and Bottas were separated by three-tenths of a second at the finish line.
After his moment in the spotlight, Vergne found his Toro Rosso undriveable as the brake temperatures went awry, but finished ninth ahead of Button who pushed him hard before dropping back and fending off Adrian Sutil's Sauber for the final point.
Kevin Magnussen also started from the pit lane and had a strong race which then got derailed by the second safety car, and he headed home Pastor Maldonado's Lotus, Daniil Kvyat's Toro Rosso and the duelling Marussia duo of Jules Bianchi and Max Chilton who were separated by half a second at the flag.
Besides Ericsson, Grosjean and Perez, Nico Hulkenberg's record of finishing each race this year ended after a collision with his team mate in Turn 13, while Caterham's Kamui Kobayashi and Sauber's Esteban Gutierrez succumbed to mechanical problems.