The Swedes, who had enjoyed an undefeated run through the preliminary round to claim a bye into the final eight, were stunned by a Slovakian squad that will now face hosts Canada in Friday's semi-final.
Swedish goaltender Henrik Lundqvist, who had not conceded a goal in the Olympics after preliminary-round shut outs against Finland and Belarus, surrendered four from only 14 Slovakian shots.
Slovakia took a 3-2 lead with 48 seconds remaining in the second period when Pavol Demitra blasted a shot past Lundqvist into the far corner of the goal.
Slovak defender Michal Handzus blocked Lundqvist's view of Demitra on the play until the shot was already past him.
Tomas Kopecky tapped in a rebound to put Slovakia ahead 4-2 with 10:59 remaining, but just 38 seconds later, Daniel Alfredsson answered for Sweden, who kept up the pressure to the finish could not net the equalizer.
Marian Gaborik, who shares fourth place among this season's National Hockey League goal scorers with 35, and Andrej Sekera, scored 37 seconds apart early
in the second period to give Slovakia a 2-0 lead.
Gaborik became the first player in the Olympics to put the puck past his NHL New York Rangers teammate, aided by a screening move from Handzus just 7:34 into the period.
The Swedes answered with two goals of their own that were 37 seconds apart.
Patric Hornqvist slotted the puck past Jaroslav Halak at 13:49 and Henrik Zetterberg followed when his backhand centering pass deflected into the goal
off Slovak defender Milan Jurcina.
Slovakia played without forward Lubos Bartecko, still recovering from a concussion suffered in Tuesday's 4-3 first-round playoff victory over Norway.
Sweden has thus failed in its bid to become the first team to defend an Olympic gold since the Soviet Union in 1984 and 1988 (with a unified team consisting of six of the former Soviet republics winning in 1992).