Brazil thrashed world champions Spain to set up a repeat of the 2008 Olympic women's football final against the United States in Paris on Saturday.
An Irene Paredes own goal set Brazil on their way, with Gabi Portilho scoring a second on the stroke of half-time to stun the side that many had tipped as favourites for the gold medal.
Adriana added a third for Brazil, who were still without their all-time leading scorer Marta, 38, after she was suspended following her dismissal in the Group C defeat to the Spaniards.
While Duda Sampaio headed into her own goal to give Spain hope, Kerolin raced clear in the first of 15 added minutes to score Brazil's fourth before Salma Paralluelo bundled in a late consolation.
Spain will now play Germany for the bronze medal in Lyon on Friday (14:00).
Brazil are now guaranteed to at least match their previous best showing in the competition, when they won the silver medal in 2004 and 2008 - on both occasions losing to the US.