© Getty Images
Captain Guardado, who scored two in the group stage, made no mistake from the spot to break La Sele hearts and keep under-fire coach Miguel Herrera in the job.
The Mexicans took the warning sign and made inroads toward netting the opener as Guardado let fly from range, but Esteban Alvarado tipped it away at the crucial moment to ensure parity at the break.
Herrera's side stepped up their pursuit of a goal after the interval when Peralta struck the post, before Carlos Esquivel hit the crossbar on 63 minutes as Costa Rica survived.
Carlos Vela could, and perhaps should, have broken the deadlock shortly afterwards but, after being played through by Peralta, fired well over with just Alvarado to beat as extra time loomed.
Mexico looked fresh out of ideas following their near misses in regulation, and Costa Rica had the best goalscoring chance in extra time but Campbell, after embarking on a mazy run, failed to beat Ochoa.
It proved a costly miss, as Miller fouled Peralta in the dying moments, before Guardado made him and Campbell pay as he thumped home the winner to set up a last-four clash with Panama.