Stephanie Houghton scored the winner after just 90 seconds in front of a largely partisan 70,584 spectators, which was the third-largest crowd to ever watch a women's game.
The carnival atmosphere continued to the end in an up-tempo match in which Brazil striker Marta hit the post before veteran striker Kelly Smith had a 55th-minute penalty saved by Brazil goalkeeper Andreia.
"I just didn't want to come in at the end. It was a bit surreal, I just wanted to sit on the pitch and lap it all up," Smith said. "It was a special night for everyone involved."
Both teams had already clinched spots in the quarterfinals but Britain's victory provides an extra confidence boost for the medal favourites, plus an easier opponent in Canada on Friday. Brazil's bid for gold after two straight silvers comes up against a much tougher test when it plays world champions Japan on Friday.
Britain finished the group stage with three victories, along with the United States.
Karen Carney recovered a looping ball along the right side before dribbling past three Brazilians to slip a pass through to Houghton, and she fired over goalkeeper Andreia to get the atmosphere charged up.
The attendance figure eclipsed the British record of 53,000 from 92 years ago and was passed by only the two medal matches from the 1996 Atlanta Games, with 76,489 watching the U.S. against China as the all-time biggest.
"It was a hard-fought contest but the crowd really inspired us," Britain coach Hope Powell said. "They gave us an extra edge which was great."
Brazil striker Marta led the South Americans as they regained the momentum but couldn't beat Britain 'keeper Karen Bardsley, whose hesitation in the 26th minute nearly cost the hosts as she allowed Marta to gather and fire an angled shot toward the open goal only for it to hit the post.
The tempo was end-to-end with the Brazilians using physical play to cut off many of Britain's counters, while the hosts did create chances through slick passing around the area as Eniola Aluko was picked out in the 55th but fired wide.
Aluko was put clear on goal moments later and was brought down by Francielle as she stepped into the area to set up the penalty. Andreia showed no hesitation in picking out Smith's shot and let out a roar after the save.
Brazil wanted a penalty after Houghton tangled with substitute Daiana inside the area in the 70th, while Rosana's 90th-minute stab at goal flew straight into the arms of Bardsley.
"The whole group did not play well," said Brazil coach Jorge Barcellos, who kept his players on the field for some time after. "We didn't do much of what we had trained for. But we have only lost a game, not the championship or a medal."
Paul Logothetis can be reached at: www.twitter.com/PaulLogoAPSuggest a correction