After a feisty but goalless first period, Spain hit the front when a Lamine Yamal pass found teammate Dani Olmo, who produced a brilliant first-time finish to beat Manuel Neuer.
Aiming to keep their Euro 2024 dreams alive, the hosts pushed for an equaliser in the closing stages of the 90, and Germany got their rewards, with Florian Wirtz netting from close range to send the game to extra time.
It looked as if the fixture was heading towards dreaded penalty kicks until the final moments of extra time, when substitute Mikel Merino's header hit the net and sent the hosts of the competition packing.
La Roja have retained their 100% record at this tournament to date, and they will look to make it a sixth straight victory when they face either France or Portugal in the semi-finals on Tuesday night.
Following an impressive run in Euro 2024 up to this point, Die Mannschaft are the first hosts to be knocked out at the last-eight stage of a European Championships.
Magic Mikel sends Spain into the last four of Euro 2024
With the game stretched as penalty kicks approached, Marc Cucurella travelled up field with the ball and passed it out to goalscorer Olmo, who whipped an inswinging cross into the German box, where Real Madrid's Antonio Rudiger would have been most onlookers' favourite to clear away the danger.
Instead, the delivery flew over the head of the centre-back, allowing Real Sociedad man Merino to rise highest and guide a headed effort past Manuel Neuer in Die Mannschaft's net, all but handing the victory to Luis de la Fuente's troops.
Germany force extra time thanks to Wirtz strike
Hurtling towards the title of becoming the first-ever host of a European Championship to be knocked out at the quarter-finals, Germany found parity in the contest late on to force the game into extra time.
Taking up a position on the left, Maximilian Mittelstadt swung a delivery into the box which was kept in play at the back post by Joshua Kimmich, and the ball found its way to Wirtz, who struck the ball into the far corner to deny La Roja a success within 90 minutes.
Spain substitute Olmo hands La Roja the lead
Dani Olmo would have be dreaming of having an impact in the match last night, but a first-half injury for Pedri meant that the 26-year-old's magical moment arrived nearly 50 minutes after the substitutes' bench.
Robert Andrich found Jamal Musiala on the right flank and the Bayern Munich sensation seemingly struck fear into the soul of David Raum, who allowed the 21-year-old to dribble into the German box.
Musiala slowed his run for a second before feeding a pass into the path of substitute Olmo, who timed his run perfectly between the German lines and produced a first-time finish into the bottom corner past the outstretched Neuer.
Spain star Pedri forced off after scrappy opening exchanges
With two great footballing sides going toe-to-toe in Stuttgart, not many inside the MHP Arena were expecting such a scrappy battle in the middle of the park between Spain and Germany during the first 10 minutes.
Real Madrid legend Toni Kroos was guilty of two badly-time tackles on Pedri, who picked up a muscle injury as a result and was forced off the field of play.
The Barcelona prodigy will be hoping that his fitness issue is not too severe, with the 21-year-old suffering with a number of injury heartbreaks during the early stages of his career.
Is Kai Havertz the answer up front for Germany?
Heading into the final 10 minutes of the match, Die Mannschaft were pushing hard to keep their European Championship dreams alive, with an unbelievable chance falling at the feet of Kai Havertz.
Unai Simon's poor goal kick deflects off the Spanish backline and Havertz rushes through on goal and spots the goalkeeper off his line, however the Arsenal attacker's long-range chipped effort flew painfully over the bar, to the despair of those of a German persuasion.
Havertz was exceptional in many areas of his play in Stuttgart but was unable to provide the finishing touch on too many occasions to warrant a place at the top of his nation's forward line.
What's next for Germany and Spain?
Germany's search for a fourth European Championship crown has ended in dramatic fashion in Stuttgart, with Die Mannschaft now only having the start of the UEFA Nations League campaign to look forward to, with Nagelsmann's troops facing Hungary and the Netherlands in September.
On the contrary, Spain go marching on into the final-four stage of the competition as they seek to lift their fourth European title, with either France or Portugal to come in the semi-finals on Tuesday night.