Rostami, 25, a bronze medallist in the same category in London four years ago, made 217kg with his final clean and jerk for a total of 396kg, beating his world record by one kilogram.
Tian Tao of China took silver with 395kgs despite making only two of his six lifts, while Romania's Gabriel Sincraian, who lifted a total of 390kg, secured bronze after edging out Kazakhstan's Denis Ulanov.
Rostami looked supremely confident throughout, though he said he was not as sure as he looked when he went for the winning lift.
Mongolian weightlifter tests positive for testosterone
"I have lifted 225 in training, but that 217 was a challenge," the Iranian told reporters. "Anything can happen."
When he made his first two snatches with apparent ease, Rostami bowed and gestured to the crowd. However, he missed his third attempt and finished the snatch only 1kg ahead of his main rival, Tian.
Earlier, the 22-year-old Tian came desperately close to being eliminated. While Rostami cruised through his first two lifts, Tian failed at 173kg, went up by 5kg for his next attempt and missed that too.
FINALLY FOUND STRENGTH
On his third snatch, also at 178kg, he looked beaten, wobbling on his right knee and then on his left before he finally found the strength to make the lift.
Tian missed his first two clean and jerks too, evoking memories of the IWF world championships in Houston, Texas last November when he missed all three attempts, having led after the snatch.
"I was too confident and didn't keep calm in the world championships," Tian said. "Tonight I always thought I would make the third one.
"I offer my congratulations to the winner and hope we have many more chances to compete against each other. I am confident I can beat him."
Rostami coaches himself, a point he made several times to the reporters after sealing gold.
"The Iranian coaches are good but I want to make my own decisions," he said. "Nobody thought it was possible to come to Rio without a coach but here I am.
"All the time I train alone, just me in a training camp. Nobody sees me. It can make your mind go a bit crazy, but I will be here again the next time (at the 2020 Games in Tokyo) for sure."
Rostami predicted more weightlifting gold for Iran in Rio as the country has favourites in two of the remaining three men's medal events, Sohrab Moradi in the 94kg and Behdad Salimikordabiasi in the super-heavyweights.
"We will see more gold medals, and more world records for Iran," said Rostami. "Now that I have won, I'm sure others will win too."
Bronze medallist weightlifter stripped of medal for doping
Moradi secures golden double for Iran