Mexico started off a little shaky, with a back pass nearly escaping Manuel Gibrán Lajud in the 10th minute for an own-goal, although the keeper was able to recover and grab the ball before it crossed the line.
The hosts then had a good chance through Ángel Zaldívar’s header, but Keylor Navas made the save.
The breakthrough came in the 29th minute, when Bryan Oviedo sent in a cross from the left that found Joel Campbell at the back post, with his header making it 0-1 for Costa Rica.
The lead didn’t last long, however, with Mexico tying it up in the 33rd minute. Josécarlos van Rankin sent the ball across the box for Víctor Guzmán, who made it 1-1 with his volley into the upper corner.
Costa Rica retook the lead before halftime, though, as Van Rankin was called for a handball to give the Ticos a 43rd-minute penalty kick. Ruiz stepped up to take it and he converted it to make it 1-2 for Costa Rica.
Mexico made a raft of changes at halftime, and the adjusted El Tri tied the game again in the 56th minute as two of those subtitutes combined. Jürgen Damm sent the ball across the box from the end line and Henry Martín was there to head it home to make it 2-2.
Mexico then took the lead for the first time in the 71st minute when El Tri was awarded a penalty of their own. Raúl Jiménez convered the spot kick to make it 3-2 for Mexico, which proved to be the final score.
Next up for Mexico is a friendly against Chile on Tuesday, October 16.
Mexico: Manuel Gibrán Lajud (Raúl Gudiño 45′), Jesús Angulo, Edson Álvarez, Gerardo Arteaga, Josécarlos van Rankin (Jürgen Damm 45′), Erick Aguirre, Víctor Guzmán, Jesús Dueñas, Ángel Zaldívar (Henry Martín 45′), Roberto Alvarado (Jesús Manuel Corona 61′), Isaác Brizuela (Raúl Jiménez 61′).
Costa Rica: Keylor Navas, Giancarlo González, Francisco Calvo, Óscar Duarte, Bryan Oviedo (Ronald Matarrita 67′), Cristian Gamboa (Ian Smith 85′), Bryan Ruiz (Randall Leal 62′), David Guzmán (Jaylon Hadden 79′), Allan Cruz (Ulises Segura 76′), Joel Campbell (Jimmy Marín 76′), Johan Venegas.