France struck twice in the last 10 minutes to see off Spain and set up a quarter-final meeting with Brazil.
Spain went ahead through David Villa's superbly struck penalty after Lilian Thuram rashly fouled Ibanez Pablo.
France soon equalised when Frank Ribery rounded Iker Casillas after he was found by Patrick Vieira's clever pass.
With seven minutes to go Vieira powered in a header and with Spain pushing forward Zinedine Zidane broke clear for a solo effort in stoppage time.
Zidane, who struggled in France's first two group games, looks to have finally found his rhythm, prompting and cajoling his teammates to victory just as he did during their successful 1998 campaign.
France's victory also means Brazil will face their nemesis Zidane again, the player who scored twice against them in the 1998 World Cup final.
An absorbing game offering two contrasting styles, with Spain pursuing a patient passing game while France adopted a more direct approach to try to utilise Thierry Henry's pace.
Mariano Pernia went close with a dipping free-kick, though the Spain defender was soon on defensive watch as he courageously blocked a Zidane shot.
While Pernia showed bravery, his defensive colleague Carles Puyol relied on athleticism to head back to Casillas to stop Zidane's pass reaching Henry.
The Arsenal striker finally escaped the attentions of the Spain defence when he broke down the right and sent over an inviting cross that neither Vieira or Ribery could reach.
Villa's penalty briefly stopped France in their tracks.
Thuram was too eager to challenge Ibanez and Italian referee Roberto Rosetti had no hesitation in pointing to the spot.
Barthez guessed correctly, but Villa's shot - low and powerful - was unstoppable.
Five minutes before the interval France equalised with a elegant goal as Vieira's quick pass released Ribery.
Finishing has not been the Marseille's winger strongest point in France's group games, but Ribery's speed got him round Iker Casillas and his sidefooted finish eluded Pernia and Puyol as they tried to cover back.
France's equaliser saw the game shift up a notch in intensity in the second half.
Just as Vieira had released Ribery, Zidane did the same for Florent Malouda, whose lobbed effort drew a one-handed save from Casillas.
Just past the hour Ribery skipped his way past Pernia, but he was unable to find a French player with his powerful cross.
France's invigorating start to the second half prompted Spain coach Luis Aragones to make a double substitution as he brought on Luis Garcia and Sanchez Joaquin.
With 12 minutes left Joaquin ghosted past Eri Abidal before firing a shot into the side netting.
But it was France who found an extra gear in the last 10 minutes as first Vieira and then Zidane punished Spain.
It was a deflected Zidane free-kick that Vieira headed in and France's captain applied the coup de grace with a run and powerful shot that gave Casillas no chance.
The defeat was the first Spain had suffered under Aragones in 25 games since he took charge after Euro 2004.