The Spanish pairing enjoyed a flying start to the contest, breaking their rivals on two occasions as they won the first set 6-2.
It appeared as though the duo would ease to the gold medal, which would see Nadal become only the second man, after Nicolás Massú, to win a singles and doubles title at the Olympic Games.
The Chilean had won both title at Athens 2004.
Nadal, the Beijing 2008 champion, who had overcome serious fitness concerns to even reach these Olympics, was made to wait after Megea and Tecau hit back to break early in the second set.
A solid series of shots saw the Romanians maintain their advantage until they were forced to save two breaks points when serving for the set, with the pair eventually holding on to force a decider.
They maintained the momentum in the opening exchanges of the third set, with Megea and Tecau breaking serve to move into a 4-3 lead.
Rather than edge closer to the gold, nerves appeared to take hold, with Nadal and Lopez responding to draw the match level at 4-4.
The Spaniards increased the pressure on their opponents by holding serve and in front of an enthralled crowd brought up to gold medal points.
Two spectacular smashes proved enough to take Megea and Tecau back from the brink to deuce, but an error meant they would need to face a third match point.
On that occasion their smash went too long, with Nadal and Lopez able to celebrate a hard fought 6-2, 3-6, 6-4 triumph.
The United States’ Jack Sock and Steve Johnson won the bronze medal by winning 6-2, 6-4 against Canada’s Daniel Nestor and Vasek Pospisil.
After his doubles success, Nadal will now target singles glory, but faces the stern test of Argentina’s Juan Martin Del Potro in the semi-finals.
Defending champion Andy Murray won his quarter-final 6-0, 4-6, 7-6 against the American Johnson, setting up a semi-final against Japan’s Kei Nishikori.
Puerto Rico’s Monica Puig triumphed 6-4, 1-6, 6-3 over the Czech Republic’s Petra Kvitova to reach the women’s singles final.
She will face Germany’s Angelique Kerber, who beat America’s Madison Keys 6-3, 7-5.