Juanlu Sanchez came off the bench to fire Spain to a record-equaling fifth Olympic men's soccer final.
Sanchez struck in the 85th minute at Stade de Marseille to seal a 2-1 win over Morocco and set up a final against France at the Paris Games.
The moment passed so fast that I didn't even realize it, but then when you realized that you scored the goal that puts your team in the final, well, I'm really happy," Sanchez said.
Morocco led 1-0 at halftime after the tournament's leading scorer, Soufiane Rahimi, converted a penalty in the 37th.
Spain evened the score in the 65th when Fermin Lopez showed quick feet in the box and hit a left-footed shot low in the bottom corner. The Barcelona midfielder then provided the assist that set up Sanchez to sweep his winning goal into the far corner.
I think that my goal changed a bit the match, and I think it was a great match for the whole team and we deserved it," Lopez said. "And here we are.
More From This Section
Spain, which won gold at the 1992 Barcelona Olympics, lost to Brazil in the final at the Tokyo Games three years ago.
It will get the chance to make up for that and add to what has already been a successful year after the men's senior team won the European Championship last month.
It's basically a World Cup for the under 23s" Spain coach Santi Denia said about playing for the gold medal on Friday at Parc des Princes in Paris.
For Morocco, it was another painful loss at a semifinal of a major tournament. It will face Egypt for the bronze medal on Thursday in Nantes. Egypt lost to France 3-1 hours after Morocco's defeat.
Morocco became the first Arab and African nation to advance to the semifinals of the World Cup in 2022 eventually taking fourth place.
We were really motivated today to play in the final. What has happened has happened, Morocco coach Tarik Sektioui said. And now we must be focused on the next game. There is a medal to be won. That's not nothing for us. It's very important for us that we return to Morocco from this tournament with a medal.
Playing in front of a fervent crowd, largely comprised of Morocco supporters, it threatened another upset at these Games after already beating Argentina in the group stage.
Rahimi had scored five goals going into the game and took his total to six when he coolly fired down the middle to beat Spain goalkeeper Arnau Tenas from the spot.
Spain came close to leveling in first-half stoppage time when Alex Baena's 25-yard effort was deflected onto the outside of the post.
The equalizer came through Lopez' clinical finish his fourth goal of the tournament and his over-exuberant celebrations earned him a yellow card after kicking the corner flag and breaking it in two.
Morocco had come close to a potential late winner before Sanchez sealed the victory for Spain, which has taken silver on three occasions, with its only gold coming in '92.
Brazil is the only other men's team to have played in five finals at the Olympics, including the past three.
Spain's women's team is also into the semifinals it faces Brazil on Tuesday, also in Marseille.