After normal time failed to separate the two teams, it took two awful indidivual defensive errors to hand El Tricolor the win and a place in the semi-finalsMexico booked their place in the final four of the Olympic Games with a hugely entertaining 4-2 victory over a spirited Senegal side, courtesy of two extra-time goals.
The pre-match favourites, Mexico, raced into a two-goal lead after an hour, with Jorge Enriquez heading home from a Giovani Dos Santos free-kick, before Javier Aquino tapped the second in to an empty net after some poor defending from Senegal.
It took a marvellous comeback to take the game into extra-time, with Pape Souare pivotal, setting up both goals for Moussa Konate and Ibrahima Balde with his superb delivery from out wide.
In extra-time, it was Mexico who took the lead, through Tottenham midfielder Dos Santos, who seized upon a Papa Gueye mistake at the back before calmly curling the ball into the bottom left-hand corner before Hector Herrera converted with another header after an Abdoulaye Ba mistake at the back.
Mexico coach Luis Fernando Tena made just the one change to the side that beat Switzerland 1-0, with Israel Jimenez coming in for Nestor Vidrio at right-back, while the rest of the side stayed the same.
Senegal coach Joseph Koto rang the changes, though, bringing back in Saliou Ciss and Abdoulaye Ba into defence, with giant striker Kalidou Yero handed his first start of the tournament up front. Stephane Badji, Magaye Gueye and Kara Mbodji were the players to make way, returning to the bench.
Mexico started the brighter of the two sides and Marco Fabian struck the crossbar after some dithering in the box between the Senegal defenders in the third minute, with Mane rooted to the spot, before it bounced away clear.
They didn't have to wait much longer, though, with Enriquez flicking on a deft header into the far corner from the near post in the 10th minute, after an excellent free-kick delivery from Dos Santos on the right-hand flank to hand them a deserved lead.
With Mexico dominating the early stages, looking comfortable on the ball and dominating possession, it took until the 16th minute for Senegal to make any sort of impact further forward, with the tournament's top goalscorer, Konate, bringing out a smart stop from Jose Corona from outside the box.
Senegal slowly began to come into the game more and more and had they had a decent effort through Konate from a Souare corner in the 24th minute, which Corona acrobatically turned behind as the game began to open up.
Oribe Peralta should have extended the lead in the 42nd minute after nodding narrowly wide unmarked in the middle of the box from a fine Fabian centre, but they went in at the break just a goal to the good in what was an absorbing first half of end-to-end action.
The second half started at a much more subdued tempo, with Senegal creating the first opportunity five minutes after the restart, with Kouyate going close, dragging his shot wide of the left-hand post from outside the box.
This seemed to bring the game back to life and Dos Santos and Fabian combined brilliantly with a series of one-twos before overcooking it as they closed in one goal to waste a gilt-edged chance.
Senegal nearly equalised moments later through Souare, who nodded his free header at the back post from Zargo Toure's fabulous cross straight at Corona as Mexico sought to extend their run of not conceding a goal throughout the entire tournament to date so far.
But it was Mexico who grabbed that crucial second goal of the game in the 63rd minute, with Aquino finishing with aplomb into the gaping open net after Senegal struggled to clear their lines and Peralta toe-poked it off Mane into the diminutive midfielder's path.
Senegal responded well and almost got a goal back with Konate volleying the ball towards goal from a Souare cross that was slightly behind him, but Corona comfortably held on.
In an almost carbon copy move, though, Senegal managed to get a goal back in the 68th minute with Konate again the goalscorer, nodding home from a superb Souare cross after pulling away from his marker Diego Reyes to keep their semi-final hopes alive.
With Mexico on the ropes, substitute Balde's overhead-kicked cross found Sadio Mane unmarked in the middle, but his diving header fell fractionally wide of the left-hand post, not before Balde missed a decent chance from a Toure low cross as Senegal began to flood forward.
Senegal sensationally equalised in the 76th minute with Balde jumping the highest to nod home another wonderful Souare corner as Mexico threw away their two-goal lead, for their second goal in a frantic seven-minute spell.
Souare was fortunate to stay on the pitch a few minutes later, though, after a flying challenge on substitute Herrera saw the Senegalese player kick the Mexican midfielder in the head, with referee Mark Clattenburg bizarrely only awarding a yellow card for the dangerous foul.
The final chance of the ninety minutes saw Perlata lose his his marker at the near post and head over at pace, an accurate Dos Santos cross deep into injury-time, but the scores remained level with nothing to separate the two sides.
Mexico began extra-time full of purpose, with Peralta and Herrera going close, not before Fabian brought a great save out of Mane, keeping the ball out with his legs after the winger's half-volley at the back post.
Nevertheless, it was Mexico who got the next goal, with Dos Santos pouncing on a Gueye error at the back before coolly slotting home past the onrushing Mane in the 97th minute to take the lead for again for the second time in the match.
Senegal were still creating chances, though, and Mane's stinging volley in the 103rd minute from inside the box saw Corona make a stunning save low to his right to maintain the team's narrow lead.
The game was put to bed in the 108th minute, when after breaking from a free-kick up the other end of the pitch, Ba's header back to his goalkeeper was terribly mis-judged, lacking any pace on it. Even though Mane handled outside of the box to deny substitute Raul Jimenez, Herrera was on hand to head the ball into the empty net to restore his team's two-goal advantage.
Mexico emerged the victors in an enthralling contest full of twists and turns, and they clinically capitalised on two errors from Senegal in extra-time to ensure they kept their medal hopes alive, in what will go down as the most memorable game of the competition so far.