Germany followed England out of the European Under-21 Championship at the group stage after Alvaro Morata's late winner for Spain in Israel.
But Germany were stunned when Real Madrid striker Morata cut inside the box in the 86th minute and drilled the ball into the net.
Spain progress to the semi-finals.
They will be joined in the last four by the Netherlands, who showed their title credentials with an emphatic 5-1 win over 10-man Russia in the European Under-21 Championship Group B.
After falling behind to Georginio Wijnaldum's 30-yard finish in the 39th minute, Russia had Nikita Chicherin sent off for a foul on Adam Maher.
Luuk de Jong made it 2-0 in the 62nd minute, Denis Cheryshev reduced the deficit, before Ola John made it 3-1.
Late goals by Danny Hoesen and Leroy Fer sealed the win for the Netherlands, who won the tournament in 2006 and 2007.
The result means Russia, who lost 1-0 to Spain in the first game, are knocked out at the group stage.