Maksim Nedasekau of Belarus took bronze, having also jumped at 2.37m, but having more failed attempts across the competition.
Tamberi and Barshim embraced before running to celebrate with their coaches and team-mates, hoisting their nation's flags above their heads.
Barshim can add the gold medal to his back to back world titles, and is the winner of Qatar's second-ever Olympic gold medal, after weightlifter Fares Elbakh won the first on Saturday in the men's 96kg event.
Tamberi was still celebrating on the track when his compatriot Lamont Marcell Jacobs ran into his arms, following his surprise win in the men's 100m final.
Both athletes have had to overcome tough injuries in their careers, but Barshim says the sacrifices were worth it.
"It is amazing. This is a dream I don't want to wake up from," he said.
"I have been through a lot. It's been five years that I have been waiting, with injuries and a lot of setbacks. But we are here today sharing this moment and all the sacrifices. It's really worth it now in this moment."
Tamberi had to take time out to recover from a career-threatening injury that ruled him out of Rio 2016.
"After my injuries, I just wanted to come back, but now I have this gold, it's incredible. I dreamed of this so many times," said the Italian.
"I was told in 2016 just before Rio there was a risk I wouldn't be able to compete any more. It's been a long journey."