With the score 0-0 in the eighth minute of stoppage time in Rabat, referee Jean-Jacques Ndala awarded Morocco a penalty for a challenge by El Hadji Malick Diouf on Brahim Diaz.
Moments earlier, Ndala had ruled out a Senegal goal.
After the penalty was given, Senegal head coach Pape Thiaw encouraged his team to leave the field in protest, with only Sadio Mane remaining.
The match was paused for 17 minutes, during which Mane encouraged his team-mates to return.
When they did, Diaz’s Panenka-style penalty was easily saved by Senegal goalkeeper Edouard Mendy – and the match went to extra time, when Pape Gueye scored Senegal’s winning goal.
During the match, there were clashes between Moroccan ball boys and Senegal reserve goalkeeper Yehvann Diouf, as he repeatedly attempted to prevent them from stealing Mendy’s towel from next to the goal.
Videos showed Diouf being tackled to the ground by three of the ball boys then dragged around on the floor as he tried to keep hold of the towel so Mendy could dry his gloves.
