Argentina looked to have rescued a point having come from two goals down when Cristian Medina headed home in the 16th minute of second half injury-time.
However, crowd trouble in the Stade Geoffroy-Guichard halted proceedings, with referee Glenn Nyberg taking both sets of players off the pitch.
The game was subsequently suspended for nearly two hours and once it resumed, the controversy and confusion continued.
Following a VAR review, Bruno Amione was adjudged to have been in an offside position in the build-up to the potential equaliser as Morocco's advantage was reinstated.
The remaining three minutes of the contest played out in front of an empty stadium, with Tarik Sektioui's side able to hold on for an unlikely triumph.
The game finished more than four hours after it started, leaving Mascherano furious at full-time, describing the outcome as not befitting an event as prestigious as the Olympics.
“The match was suspended for security and safety,” Mascherano began. “At no moment did we talk about the review. The official Olympic page put the match at 2-2.
“The match was suspended for security. At no moment did we talk about the review. Then after we were not told anything new, we started to hear things.
“The referee never came, he never gave us an explanation.”
He added: “We turn the page and stay with the positive things from the team in the second half and look for the two wins we’re going to need.
“What happened on the pitch was a scandal. This isn’t a neighbourhood tournament, this is the Olympic Games.
“How are they going to stop the match seven times in the second half because someone comes onto the pitch?
“The second and third time you have to take a decision. Seven times people came onto the pitch.”
Lionel Messi, who won gold for La Albiceleste in Beijing in 2008, also shared his frustrations with his former Barcelona team-mate.
The 37-year-old kept it to the point, posting “Insolito” on his Instagram story, which translates to “unbelievable".
Argentina face Iraq in their next game in Lyon on Saturday, concluding their group campaign against Ukraine on July 30.
Morocco were on course for victory when Al Ain striker Soufiane Rahimi scored twice in a five-minute spell either side of half-time, before Giuliano Simeone halved the arrears for Argentina.
Cristian Medina appeared to have equalised deep into stoppage time, but crowd trouble caused referee Glenn Nyberg to take the teams off the pitch before a VAR review could take place.
It was ruled that Bruno Amione was offside in the build-up, and with the goal chalked off, the teams returned to play three further minutes in an empty stadium two hours later, Morocco holding on for a memorable win.
Morocco inched ahead two minutes into first-half stoppage time, Ilias Akhomach bamboozling the Argentina defence with a wonderful backheel that allowed Bilal El Khannouss to cross for Rahimi to tap home.
Within four minutes of the restart, Morocco had a chance to double their lead as the tricky Akhomach was pushed inside the area by Julio Soler. Rahimi stepped up to the spot and made no mistake, stroking his kick into the bottom-left corner.
Javier Mascherano's team dragged themselves back into the contest with 22 minutes to play, though, as Soler's cross-shot was prodded in by the stretching substitute Simeone.
It looked like Argentina had levelled 16 minutes into stoppage time as both Amione and Nicolas Otamendi struck the crossbar before Medina nodded home from almost on the goal line.
However, some spectators then threw cups, bottles and pyrotechnics at the celebrating Argentina players, causing the teams to leave the field for almost two hours as fans were told to leave the stadium.
With a VAR review ruling Amione was offside in the build-up to Medina's header, the game restarted at 2-1 to Morocco and the Atlas Lions clung on for a huge win.
Morocco looked set to open their Group B campaign with a memorable win when Al Ain striker Soufiane Rahimi scored twice in a five-minute spell either side of half-time.
However, substitute Giuliano Simeone halved the arrears for Javier Mascherano's men, who twice hit the woodwork before Medina nodded home with the final contribution to an incredible game.
Morocco inched ahead two minutes into first-half stoppage time, Ilias Akhomach bamboozling the Argentina defence with a wonderful backheel that allowed Bilal El Khannouss to cross for Rahimi to tap home.
Within four minutes of the restart, Morocco had a chance to double their lead as the tricky Akhomach was pushed inside the area by Julio Soler. Rahimi stepped up to the spot and made no mistake, stroking his kick into the bottom-left corner.
Mascherano's team dragged themselves back into the contest with 22 minutes to play, though, as Soler's cross-shot was prodded in by the stretching substitute Simeone.
The Albiceleste struggled to carve out clear-cut chances to equalise, but Medina saved the day in dramatic circumstances with 116 minutes on the clock.
Munir Mohamedi spilled Thiago Almada's long-range shot then tipped Bruno Amione's follow-up onto the woodwork. Nicolas Otamendi then looped his header against the crossbar but Argentina made sure at the fourth attempt as Medina nodded into an unguarded net.
Data Debrief: Albiceleste leave it late
Medina's leveller was officially timed at 105 minutes and 24 seconds, the lengthy period of stoppage time a result of several heavy challenges and a number of supporters encroaching on the playing field throughout the match.
Few could argue they did not deserve their point, though, having ended the game with a tally of 2.41 expected goals (xG) to Morocco's 1.46.
Abde Ezzalzouli and Soufiane Rahimi impressed for the Atlas Lions, with their first half goals sending Tarik Sektioui's on their way to a podium finish at the Games.
Bilal El Khannouss, Rahimi, Akram Nakach and a stunning free-kick from captain Achraf Hakimi would add further gloss to an impressive display after the break.
Morocco got off to an excellent start through Ezzalzouli's curling effort from outside the box, as Rahimi added a second three minutes later with a powerful header.
The result was put beyond doubt when El Khannouss notched Morocco's third, with Rahimi netting his sixth goal of the tournament just after the hour mark.
Rahimi then turned provider to set up Nakach inside the area, with Hakimi rounding off a fine display in style with an exquisite dead ball effort from distance.
Data Debrief: Rahimi stars in Atlas Lions' landmark triumph
12 years after their last appearance at the Olympic Games in London, Morocco returned with a bang, ending an impressive campaign with a deserved bronze medal.
Rahimi, who plays his football in the UAE with Al Ain, has been arguably the standout player of the tournament and leads the race for the Golden Boot ahead of France's Jean-Philippe Mateta and Spain's Fermin Lopez.
Morocco outperformed their expected goals (xG) by 4.76, with Rahimi contributing 0.82 to their xG total (1.24), while also registering the most shots on target (two) and touches in the opposition box (seven).
Soufiane Rahimi had given Morocco the lead from the penalty spot in the first half, but an inspired second-half performance from Fermin saw him score before assisting Juanlu Sanchez to earn the victory.
In the process, he became the youngest player (21 years and 86 days) to both score and assist in a men's Olympic tournament knockout match since Neymar against Honduras in 2012.
La Roja are into the Olympics final for the second consecutive tournament, having claimed the silver medal in Tokyo after a 2-1 defeat to Brazil in the final.
And Fermin believes Spain deserved to reach the showpiece after their resilient performance in the second half.
"I'm very happy to be in the final of the Olympics. The team deserved it, now we're thinking about the final," Fermin told AS.
"The game was difficult for us, they were putting a lot of pressure on their fans, it was a nice game and I think we deserved the victory.
"I hope I can play in every game. The most important thing is that we have taken a step forward, and we have achieved victory.
"After the break we were ourselves. I always try to help the team, whatever the manager asks of me. I'm very happy with our football and I hope we can win all the medals possible."
Spain will play either France or Egypt in the gold-medal match at the Parc des Princes on Friday.
Soufiane Rahimi had given the Atlas Lions the lead with a penalty in the first half before Fermin Lopez restored parity in the second.
Fermin started brightly for Spain, testing Munir Mohamedi with a long-range strike before dragging an effort just wide of the far post after darting in behind.
Shortly after, Spain found themselves behind – Pablo Barrios caught Amir Richardson on the back of the calf with a high tackle in the box and Rahimi made no mistake from the spot to fire Morocco in front.
La Roja dominated the second half as they pushed for an equaliser, and Fermin clawed them level after jinking away from his marker to nestle his low shot into the bottom-right corner after 66 minutes.
Richardson almost restored Morocco's lead moments later, but his first-time effort swerved away from goal and wide of the near post, much to his frustration.
Substitute Sanchez then finished the comeback for Spain from a tight angle, with his shot bouncing in off the inside of the far post.
Abde Ezzalzouli and Richardson gave Arnau Tenas a late scare in goal with two powerful efforts, but luckily for the keeper, they could not find the target.
Data Debrief: Fermin shines bright
Even in the first half when Spain struggled to make an impact, it felt like Fermin could prove to be the difference for La Roja, and he did just that.
At 21 years and 86 days, he is the youngest player to both score and assist in a men's Olympic tournament knockout round match since Neymar against Honduras in 2012 (20y 181d).
Santiago Sanchez will be hoping he can provide the spark once more in the showpiece game on Friday.
Achraf Hakimi scored a stunner, with Soufiane Rahimi, Ilias Akhomach and El Mehdi Maouhoub also netting in an emphatic victory as the USA failed to mount a threat.
Nathan Harriel upended Rahimi in the box just before the half-hour mark and the forward made no mistake from 12 yards, stroking the ball just out of Patrick Schulte's reach into the bottom-left corner.
The USA should have equalised in the 59th minute but Walter Zimmermann's nod-down was skewed well wide by Miles Robinson from close range.
Just four minutes later, Morocco doubled their lead – Akhomach darted in behind to rifle Abde Ezzalzouli's drilled cross in at the near post.
Hakimi will take most of the plaudits though, with the Paris Saint-Germain star winning an aerial duel before carrying the ball from the halfway line to the edge of the box and picking out the bottom corner with pinpoint accuracy.
Harriel's sorry day continued as he was penalised harshly for a handball after a VAR check in the 90th minute, and Maouhoub emphatically fired in Morocco's fourth.
Data Debrief: Atlas Lions roar
Tarik Sektioui could not have asked for a better performance from Morocco, as they outplayed the USA at the Parc des Princes.
They created 2.53 expected goals from their 15 shots, hitting the target with eight of those, and converted all three of their big chances.
One thing is for sure, they will feel very confident going into the final four where they will face either Japan or Spain.
Argentina looked to have salvaged a 2-2 draw from 2-0 down in Saint-Etienne, with Soufiane Rahimi netting twice before Giuliano Simeone pulled one back for La Albiceleste.
Medina appeared to have equalised when he nodded home following a goalmouth scramble 16 minutes into second-half stoppage time, but crowd trouble then broke out with cups, bottles and pyrotechnics being thrown at Argentina's players by spectators.
The referee took the teams off the pitch, and amid confusion over whether the final whistle had sounded, it was reported another three minutes would be played in an empty stadium.
It was subsequently announced Argentina's potential leveller had been ruled out after a VAR review showed Bruno Amione was offside in the build-up.
Another three minutes of stoppage time were then played out around two hours after the players had been taken off, with Morocco holding on to win 2-1.
While Morocco now sit top of Group B with three points, Javier Mascherano's side – who were among the pre-tournament favourites – remain on zero.