Brazil cruised into the final of the Olympic Games with a 4-2 victory over Spain at the Orange Velodrome on Tuesday.
Salma Paralluelo netted two late consolations, but the damage was already done as Irena Paredes' own goal, Gabi Portilho, Adriana and Kerolin denied the world champions a place in the gold-medal match.
Brazil were still without legend Marta after she lost her appeal over a two-game ban, but her team-mates ensured she would bow out of Olympics action with the chance to play for the top prize.
Spain were behind after just six minutes as Cata Coll's poor clearance struck the unfortunate Paredes on the side to send it cannoning over the line.
La Roja struggled to gain a foothold, and Brazil doubled their advantage on the stroke of half-time thanks to Portilho's composure when one-on-one with Coll.
Adriana made sure of their progress off the bench in the 71st minute – she struck the bar but was then on hand to head Portilho's nod-down in from close range.
That sparked Spain into life and Paralluelo's looping header snuck in at the far post before Alexia Putellas rattled the crossbar and forced Lorena into a smart stop to slow down the comeback attempts.
Kerolin delivered the hammer blow in the opening seconds of stoppage time by sliding her low shot through Coll's legs, though Paralluelo would have the last say, poking in her second of the match from close range in the 112th minute.
Data Debrief: World champions denied
Spain were aiming to become the first side to win Olympic gold after lifting the Women's World Cup, but they just fell short at the final hurdle after being outplayed.
La Roja's late flurry of chances meant they finished with more shots than Brazil (26 to their 17) and more on target (11 compared to seven).
Brazil's dominance showed in other ways though, as they created an expected goals (xG) of 3.27 and had a total of six big chances, as they secured a meeting with the United States in the final on Saturday.