Reigning European champions Spain had not lost a qualification game since a 2-0 reverse in Sweden back in October 2006, but Stoch's 87th-minute header gave Jan Kozak's side all three points in Thursday's Euro 2016 qualifier in Zilina.
Iker Casillas' blunder had allowed Juraj Kucka to put Slovakia 1-0 up at the break, but substitute Paco Alcacer appeared to have spared his captain's blushes with an equaliser eight minutes from time.
However, there was to be one last twist when Stoch nodded home Michal Duris' excellent delivery at the back post to give Slovakia a memorable win.
Casillas' latest error, which saw him allow Kucka's long-range strike to beat him straight down the middle, is likely to increase the pressure on Spain boss Vicente del Bosque to drop the Real Madrid man in favour of in-form Manchester United stopper David de Gea.
Diego Costa was named in Spain's starting XI despite fears over his ability to play due to a persistent hamstring injury, but he cut an isolated figure in the early stages and Casillas was forced into a stunning one-handed save to deny Robert Mak after 11 minutes.
At the other end Cesc Fabregas and Costa, who have combined so effectively for Chelsea this season, created Spain's first clear-cut effort when the latter glanced a header wide from the former's cross.
And that miss was punished in the 17th minute when Slovakia took the lead.
Vladimir Weiss laid a short free-kick to Kucka, whose thunderous 30-yard strike wrong-footed Casillas despite going straight down the middle.
Spain finished the half strongly and Andres Iniesta forced a smart stop from the outstanding Matus Kozacik, before the Slovakia keeper made a superb double save to deny Costa and Sergio Busquets.
The visitors showed far more urgency after the break and Fabregas' firm effort produced another top save from Kozacik after good build-up play by Busquests and Juanfran.
Costa, too, seemed livelier, turning sharply in the area and firing a shot low towards to the bottom left-hand corner, but Kozacik was once more up to the task.
But he was finally beaten in the 82nd minute when Alcacer - who came off the bench to replace David Silva - controlled Jordi Alba's delivery and slotted home his second goal in three senior international appearances.
However, just as Spain thought they had done enough to preserve their long unbeaten streak in qualifiers, Stoch converted Duris' inch-perfect delivery to send the home fans into delirium.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.