Brazil legend Marta will get the chance to end her glittering career with Olympic soccer gold after her teammates set up a final against the United States, beating world champions Spain 4-2 .
Gabi Portilho and Adriana struck at Stade de Marseille after an own goal from Irene Paredes had put the two-time silver medalist in an early lead in Tuesday night's semi-final.
The 38-year-old Marta was suspended for the quarter-finals and semi-finals and has said her sixth Olympics will be her last major tournament with the national team.
Brazil will play the four-time champion US in Saturday’s final at Parc des Princes in Paris.
Spain missed their chance to further cement their place at the top of women's soccer by adding Olympic gold to the World Cup they won in Australia last year.
They will play Germany for bronze, with the US beating Germany 1-0 in the other semi-final in Lyon.
The Americans, undefeated in France under new coach Emma Hayes, will be vying for their fifth gold medal in their sixth appearance in the Olympic women's soccer final.
But it took an extra-time goal from Sophia Smith to send them to the gold-medal match.
Smith broke the scoreless stalemate five minutes into extra time, out manoeuvering defender Felicitas Rauch and German goalkeeper Ann-Katrin Berger.
“I saw like a little opening of net and I was just like, 'I've got to put it there. Just put it there,'” Smith said. “It was a good feeling. I know I had a few other chances this game that I should have put away. But sometimes one is all it takes.”
The United States had missed out on the finals at the last two Olympic tournaments, eliminated in the quarterfinals in 2016 in Rio de Janeiro and relegated to the bronze-medal match at the Tokyo Games three years ago.
In Marseille, Paredes' sixth-minute own goal put Brazil in control and Portilho doubled the advantage in the fourth minute of first-half stoppage time.
Adriana's header in 71st underlined Brazil's dominance.
Salma Paralluelo pulled a goal back for Spain with a header in the 85th and almost immediately Alexia Putellas struck the bar with a shot from outside the area.
Kerolin made it 4-1 in the first minute of stoppage time - shooting through Coll’s legs — before Paralluelo got her second in the 12th minute of stoppage time. By then, though, it was too late for Spain to mount a comeback.
Brazil have been twice beaten in an Olympic final by the US - at Athens 2004 and Beijing 2008.
They secured a place in the final for the third time after making amends for a 2-0 loss to Spain in the group phase at the Paris Games.
That was a game that also saw Marta red-carded, and Brazil received confirmation just hours before kick off in the semi-finals that their appeal against her ban had been rejected.