Sweden ended a disconsolate Marta's dreams of guiding Brazil's women to a first Olympic football gold medal with a 4-3 penalty shoot-out win to set up a final with Germany.
The five-time world player of the year walked off in tears after the upset before 70,000 people in a stunned Maracana stadium.
Brazil thrashed the Swedes 5-1 in the group stages but were held to a third consecutive 0-0 draw. As in their quarter-final win over Australia, they were forced to go through the agony of penalties.
Unlike against the Australians, Marta converted from the spot, but then saw her ambition of capping a glorious career by finally winning a major international title devastated.
"There is no way to describe the feeling of frustration," said Brazil coach Vadao.
Marta, who has played her club football in Sweden for the past four years, was consoled by Swedish players, whilst the huge Maracana crowd gave their defeated team a huge ovation.
Cristiane, the Olympics' all-time top scorer, and Andressa saw spot-kicks saved by Hedvig Lindahl after neither side could score a goal in 120 minutes.
Lisa Dahlkvist converted the winning penalty to secure a place in the final and Sweden's first women's football Olympic medal.
Germany also avenged a group stage defeat in the other semi-final. Goals from tournament top scorer Melanie Behringer and Sara Daebritz confined Canada to a 2-0 defeat and another battle for the bronze they won in London four years ago against Brazil in Sao Paolo on Friday.
Brazil's women were playing for just the second time at Maracana and Marta hopes the momentum built-up for the women's version of the game in football-mad Brazil is not halted by the disappointment.
"Nothing will take the shine off the work we did during the whole year," Marta told SporTV.
- 'Brazil means football' -
"This is a glorious moment for women's football. It is everybody's dream to play here," added Vadao.
"It is full of tradition. It is a cultural thing in Brazil, it is a symbol. Maracana means Brazil and Brazil means football. It is quite a moment and I am just sorry we didn't make it to the final."
There was delight for Sweden coach Pia Sundhage, who can now win a third straight gold medal after leading the United States to victory in 2008 and 2012.
"It is very emotional. I have been in China and London in Olympic finals, but with a team everyone expected to do well.
"The road we have taken has been different, a wonderful trip with one or two upsets."
Sweden's hero Lindahl described the baking sun in Salvador during the Scandinavians' shock penalties quarter-final win over the USA three-time gold medallists as "her enemy".
Lindahl suffers from vitiligo, a condition that causes patches of skin to lose their pigment, making the skin particularly sensitive in the sun.
And she was under fire in the Rio heat as early Brazilian attacks peppered the Sweden goal.
Lindahl even had to endure chants of "Zika" from the home fans, which had been previously reserved for outspoken USA 'keeper Hope Solo.
Marta inspired the 5-1 group stage thrashing of Sweden with two goals, but despite their dominance the ball would not go in for the hosts as Marta, Beatriz and Debinha all came close.
Solo castigated Sweden as "cowards" for their defensive display in the last eight, but that seemed to only strengthen the resolve of Sundhage's women as they barely threatened the Brazilian goal.
"It is not for me to criticise the opponent, especially because their strategy worked and they had good results in both matches," said Vadao graciously.
Vadao introduced Cristiane for extra-time despite doubts over her fitness after missing the quarter-final through injury.
The gamble backfired, though, as she couldn't add to the 14 goals she has scored across four Games from the spot. And when Lindahl parried Andressa's effort, Dahlkvist coolly slotted home to silence the Maracana.