Then a throw-in by Peter Pekarik caught Italy off guard and Kamil Kopunek were quick enough to lob the ball over Italian keeper Federico Marchetti.
Quagliarella cut Slovakia's advantage with a cannon shot two minutes into the stoppage time but Simone Pepe wasted their last chance when he struck wide at the far post.
Italy coach Marcello Lippi took all the blame by himself.
"I assume complete responsibility for this failure," he said. "I was convinced this squad could do a lot better than that - not win the World Cup, but do a lot better.
"When a team goes into a match as important as this with fear in the stomachs and paralysis in their legs, it's because the coach hasn't prepared them well enough mentally, physically and tactically, but above all mentally."
The gray-hair guru coach was emotional in the post-game press conference.
"I offer my apologies to Italian sport, the players, the football association - everyone," he said. "I'm very sad that my adventure with the Italian football association has come to an end in this fashion.
"Thank you to everyone for all my years at the helm of La Nazionale and good luck to my successor."
Slovakia coach Vladimir Weiss shed happy tears after the epic win.
"This is a fantastic day for Slovakia and the second greatest day of my life after the birth of my son," he said.
Paraguay topped Group F with five points following a scoreless draw with the Kiwi side who collected three points, one behind Slovakia. Italy were last with two points from as many draws.
Japan joined fellow Asians South Korea in last 16 thanks to early goals by Keisuke Honda and Endo.
Denmark, who had never failed to reach the second round in their World Cup appearances, fought end to end with Japan before Honda opened the Japanese account in the 17th minute.
A free-kick by the CSKA Moscow midfielder about 30 meters from the goal flew over the defense line of the Danish Dynamite and slipped away from the fingertips of goalkeeper Thomas Sorensen.
At half an hour, with Sorensen expecting Honda to take a set piece, Endo stunned him by curling the ball around the wall and inside the post.