Second-ranked Russia dropped the first two sets before denying top-ranked Brazil match point twice to spark a comeback in the 19-25, 20-25, 29-27, 25-22, 15-9 victory, paced by middle blocker Dmitriy Muserskiy with 31 points.
The Russians thought they'd won it on Muserskiy's kill in the fifth set and began to celebrate, but the officials awarded the point to Brazil. After regrouping, Muserskiy came right back to spike match point.
The win gives the Russians four gold medals in volleyball, most of any nation. But the last time Russia was atop the podium was at Moscow, when the former Soviet Union won gold on home soil.
Russia's victory denied Brazil a dual gold with its women's team, which beat the United States a day before in their Olympic final at Earls Court.
After thwarting Brazil in the third set, Russia rode the momentum to fifth, going up 8-4 on Maxim Mikhaylov's kill. They piled to celebrate the victory when they finally won it for sure.
Wallace de Souza led Brazil with 27 points.
Brazil looked to be heading into the 2016 Rio Games as the defending champion, going 18-12 on Lucas Saatkamp's ace and taking the set on de Souza's kill.
Saatkamp's block gave Brazil a 19-15 advantage in the second set. The fans were in a frenzy when Dante Amaral's kill made it 21-17, and Sidnei dos Santos Junior raised his arms to urge them on.
Amaral spiked to put Brazil up 2-0.
But Russia fought back, pulling even twice before denying Brazil two opportunities at match point. Brazil held off set point twice until Russia claimed it on Alexander Volkov's block.
Mikhaylov's kill gave Russia a 22-15 lead in the fourth and the set was won when Murilo Endres' serve went long.
Brazil defeated Russia in the preliminary round to even the overall Olympic record between the teams at 4-4 going into Sunday's final.
Brazil coach Bernardo Rezende played 35-year-old captain Gilberto Godhoy Filho, known as Giba, at times during the match. One of the game's most celebrated stars, Giba never started in London while Rezende went with younger players.
The Brazilians were without opposite Leandro Vissotto Neves, who injured his upper right leg in Brazil's semifinal victory over Argentina.
Brazil also won the silver medal at the 2008 Beijing Games, falling in an emotional final to the United States that came after U.S. coach Hugh McCutcheon's father-in-law was killed at a Chinese tourist attraction a day before the opening ceremony.
Italy won its fourth men's volleyball medal in the last five Olympics by beating Bulgaria for the bronze earlier on Sunday. The Italians won 25-19, 23-25, 25-22, 25-21, with captain Cristian Savani scoring a game-high 23 points.