It was a story of comebacks in the semifinal matches of the men's soccer tournament. 

Spain did just enough to snag a 2-1 win late in the match against Morocco, while France needed a 3-1 win in extra time to survive an upset against Egypt. 

Spain and France will play in the gold medal match, while Morocco and Egypt will battle for bronze. 

Morocco vs. Spain

Played in an electric atmosphere, Spain showed resiliency in its 2-1 comeback win against Morocco in the first semifinal match of the tournament. 

Soufiane Rahimi got his sixth goal of the tournament after he converted a penalty in the 37th minute to put Morocco up 1-0. The African nation looked comfortable hanging on to its one-goal lead throughout the first half and the start of the second, but Barcelona's Fermin Lopez scored his fourth goal at the tournament in the 66th minute to put Spain level. 

Both sides had a fair share of the ball in the last 20 minutes of the match, but Spain proved to be the only side to capitalize on its clear-cut chance in the 85th minute. A slick pass from Lopez to set up Juanlu Sanchez in a 1-on-1 situation against Morocco's goalkeeper, and Sanchez was able to cooly put the ball in the back of the net to give his side a late 2-1 lead. 

Morocco was unable to find an equalizer in stoppage time. The majority of fans in the stands were Moroccan supporters, and the fun will not stop just yet for them as Morocco will play in the bronze medal match. 

Spain did an excellent job of limiting Paris Saint-Germain defender Achraf Hakimi from having an impact, and as a result, the Europeans will play in the gold medal match. Spain will play against the winner in the other semifinal match between France and Egypt. 

MATCH STATS

France vs. Egypt

France needed a late goal in regulation and another two in extra time during its 3-1 win over Egypt to escape an upset in the semifinals. 

It was a game played out in which many expected — France had most of the ball while Egypt sat back in its own half and prepared to hit the host nation on the counter. That plan from the Egyptians seemed to work as Saber Mahmoud scored for the African nation in the 62nd minute to lead France 1-0. Egypt continued to frustrate France, but the Europeans got their breakthrough very late in the 83rd minute as Michae Olise's pass to Jean-Philippe Mateta broke the Egyptians' backline and Mateta went on the slide the ball past the goalkeeper to equalize. France then almost had the opportunity to win the game in stoppage with a penalty as it appeared Egypt's Omar Fayed handled the ball in the box, but after a VAR check, the referee ruled that Fayed did not do so. 

Two minutes in extra time, Egypt put itself in a harsh situation as Fayed received his second yellow in the match and was sent off for a challenge on France's Desire Doue. Only seven minutes later, Mateta was able to score against the 10-man opposition to make it 2-1. At the beginning of the second half of extra time, Olise got on the scoresheet to seal France's 3-1 win and a trip to the gold medal match in Paris. 

France has cruised through most games in the men's tournament, but a wake-up call against a gutsy Egypt may just be what the team needs heading into its Olympic final match against Spain with a gold medal on the line. 

MATCH STATS