Paris - Ivan Perisic's last gasp winner gave Croatia a 2-1 win over Spain that condemned the defending European champions to a heavyweight last 16 clash against Italy while Germany claimed their place in the knockout rounds.
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.
"It was not the desired route for us," said Spain coach Vicente Del Bosque.
"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."
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.
Despite the absence of injured Real Madrid star Luka Modric, Croatia fought back. Nikola Kalinic flicked home an equaliser on the stroke of half-time.
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.
Turkey beat Czech Republic 2-0 through goals from Burak Yilmaz and Ozan Tufan, to keep alive their very slim hopes of reaching the next round.
They need Sweden and the Republic of Ireland to drop points against Belgium and Italy in Group E on Wednesday to advance as a best third-place team.