France struck twice in the last 10 minutes to see off Spain and set up a quarter-final meeting with Brazil.
Ribery scored his first goal for France
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 again face their nemesis Zidane, the player who scored twice against them in the 1998 World Cup final.
An absorbing game offered 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 an 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.
Spain: Casillas, Pablo, Puyol, Sergio Ramos, Pernia, Fabregas, Xavi (Senna 72), Alonso, Villa (Joaquin 54), Torres, Raul (Luis Garcia 54).
Subs Not Used: Salgado, Marchena, Albelda, Reyes, Antonio Lopez, Iniesta, Canizares, Juanito, Reina.
Goals: Villa 28 pen.
France: Barthez, Sagnol, Thuram, Gallas, Abidal, Ribery, Zidane, Makelele, Vieira, Malouda (Govou 74), Henry (Wiltord 88).
Subs Not Used: Landreau, Boumsong, Dhorasoo, Silvestre, Saha, Givet, Diarra, Trezeguet, Chimbonda, Coupet.
Booked: Vieira, Ribery, Zidane.
Goals: Ribery 41, Vieira 83, Zidane 90.
Ref: Roberto Rosetti (Italy).
Fifa man of the match: Patrick Vieira.
This was the 700th match in the history of the World Cup. The French were one of the combatants in the very first game and they featured in the previous centennial match four years ago.
The last time France qualified for the quarter-finals was at the 1998 World Cup, which they went on to win. Since losing to Slovakia in their first match of 2006, the French have now won five and drawn two matches, keeping four clean sheets and not conceding more than two goals in any of the seven games.
France have ended Spain's 10 match unbeaten run at the World Cup. It was also the first defeat in 26 international matches for the Spaniards, in a sequence stretching back to a 1-0 loss to Portugal at Euro 2004.
Trivia stats source: Infostrada Sports