Mario Gomez struck the only goal in a 1-0 victory over Northern Ireland in Paris to ensure Joachim Loew's men advance as Group C winners, with Poland through as runners-up following a 1-0 win over Ukraine.
Even Northern Ireland became one of the six latest nations through, stealing one of four spots for the best third placed finishers.
Spain must now beat Italy in a rematch of the 2012 final to remain in contention for a record third successive European Championship title after sliding to defeat against Croatia in Bordeaux. Turkey beat Czech Republic 2-0 in the day's other Group D game.
"That is football, it seemed like we had the game controlled, but in football you can't take anything for granted. We will try to recover from this disappointment and prepare for Monday."
More From This Section
Tuesday's results also mean that Slovakia, Hungary and Northern Ireland are all guaranteed to reach the knockout stages. Spain just needed to avoid defeat to secure top spot in Group D.
Alvaro Morata grabbed a seventh minute goal that should have had Spain cruising towards a last-16 tie with a third-place team from Group B, E or F.
Spain captain Sergio Ramos then saw his second-half penalty saved by Danijel Subasic, and it cost his side dearly as Perisic struck a winner three minutes from time to fire Croatia through in first place.
With Italy guaranteed to finish top of Group E, Spain will have to beat them and then possibly Germany to reach the semi-finals. Hosts France or England could then stand in the way of a place in the July 10 final.
starting XI after Loew's side laboured to a 0-0 draw against Poland last week. The on-loan Besiktas striker repaid his coach's faith with the only goal.
Germany dominated, with Thomas Mueller, who set up Gomez's goal, twice firing against the woodwork in the first half. Northern Ireland goalkeeper Michael McGovern pulled off several saves to limit the damage. Germany will now play Albania or Slovakia in Lille on Sunday.
"We played well and we were convincing but we didn't score nearly enough. That was really lacking today" said defender Mats Hummels. When we play against the really top teams we'll have to give it that little bit more."
"They had no chances, but we needed to have had more purpose and consistency in front of goal," Loew said.
Half-time substitute Jakub Blaszczykowski needed just nine minutes to fire Poland past the Ukraine in Marseille.
Blaszczykowski swept home a tidy finish following a cleverly-worked corner to leave Poland on seven points, level with Germany, but behind their neighbours on goal difference.
"We would have liked to have scored more goals, but when you see in the standings that we're only separated by goal difference from world champions Germany, you have to be happy," said goalkeeper Lukasz Fabianski.
For Ukraine it was a third successive loss and the last match for coach Mykhaylo Fomenko, who announced earlier on Tuesday that he would leave his post following the tournament.