The 30-year-old Spaniard added the 2016 doubles crown to his 2008 singles gold in Beijing by defeating Romania's Florin Mergea and Horia Tecau 6-2, 3-6, 4-6.
Nadal to play singles, doubles and mixed doubles in Rio Olympics
Nadal is just the fourth player to win gold in Olympic singles and doubles after Serena and Venus Williams and Chile's Nicolas Massu.
The former world number one and holder of 14 Grand Slam titles was playing his first tournament since May when a wrist injury forced an early retirement from the French Open.
"It was an amazing experience, especially doing it with one of my best friends, with Marc, it's something unforgettable for me, for both of us," said Nadal.
"To win a gold medal after two months and a half with no practice, with no preparation, just working in the gym, is something that is a dream.
Davis Cup hit by star pullouts, Nadal Rio appeal
"The moment was unbelievable, and we had fun. We fight hard."
Nadal's participation in the Olympics was in doubt until the last minute but now he has one gold and the prospect of another in the men's singles where he's reached the semi-finals.
Earlier Friday, he defeated Brazil's Thomaz Bellucci 2-6, 6-4, 6-2 for his 800th career singles win.
He will face Juan Martín Del Potro of Argentina on Saturday for a place in the championship match.
"It's a dream to win for my country. I didn't come here just to carry the flag at the opening ceremony," added Nadal.
For 34-year-old Lopez, it was a second major title of 2016 after he partnered compatriot Feliciano Lopez to the French Open title in June.
"Sharing these feelings with Rafa is really a dream come true," said Lopez, who has now won five doubles titles with Nadal.
"All the things we have done together, I am the happiest man on earth."
Tecau paid tribute to Nadal whose all-court skills proved crucial.
"You don't see this in singles, but he volleys really well. He moves a lot at the net, puts a lot of pressure, defends really well," said Tecau.
Davis Cup hit by star pullouts, Nadal Rio appeal
"He can hit an angle in every shot. And he's all over the court. You have to play with precision and play to win most of the time. Otherwise you're going to give him a lot of shots and that's what he likes. Then he takes control of the points."
The Spanish pair had cruised through the first set of the final 6-2 on the back of breaks in the fifth and seventh games.
But Mergea and Tecau, giving Romania their first ever Olympic tennis medal, hit back, focussing their attack on Lopez.
They broke for a 3-1 lead and saved two break-back points in the ninth game to level the final on a chilly night in the Brazilian city.
Mergea and Tecau, both 31 and born within a week of each other, sensed victory when they nipped ahead at 4-3 in the decider with Lopez again broken.
But the Spanish duo regained the break at 4-4 when Tecau was broken. Nadal then held for a 5-4 lead.
The Romanian pair saved two championship points in the 10th game but a wild Tecau smash gave Nadal and Lopez the title after two and a half hours of action.
The Spaniards' all-out attack and rock-solid defence were crucial forcing their opponents into making 46 unforced errors and having to save 12 of 16 break points they faced.
America's Steve Johnson and Jack Sock defeated Daniel Nestor and Vasek Pospisil, the Canadian seventh seeds, 6-2, 6-4 to claim the bronze medal.
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ