The full-back fired home with four minutes of normal time remaining after Dani Parejo had cancelled out Cristiano Ronaldo's 20th LaLiga goal of the season.
Zinedine Zidane's side were beaten 2-1 in the reverse fixture at Mestalla in February and almost found themselves a goal down inside the first minute, with Santi Mina hitting the post from inside the area.
Madrid began to settle and Ronaldo turned the ball in from Dani Carvajal's cross midway through the first half – his first goal in the top flight since March 12.
The home side missed chances to double their lead, with Ronaldo seeing a spot-kick saved by Diego Alves for the third time in four attempts, and their profligacy was punished eight minutes from time as Parejo swept in a sublime free-kick.
But Marcelo's brilliant solo goal salvaged a second win in a row since Madrid's Clasico defeat and ensured that the title race remains firmly in their hands.
Madrid were ahead in the first minute of their 6-2 thrashing of Deportivo La Coruna, but they nearly went behind with less than 60 seconds on the clock here, with Keylor Navas denying Mina from close range before the forward smacked the outside of the post with the rebound.
It was a sluggish opening 20 minutes from the home side, with Karim Benzema's turn and low shot the only moment that had Valencia goalkeeper Alves troubled in any way.
James Rodriguez – starting on the right of the attack – and Ronaldo both sent free-kicks narrowly over the crossbar, but Madrid's increasing pressure told on 27 minutes.
Carvajal bent a fine cross towards the edge of the six-yard box from the right, where Ronaldo rose highest to glance a header low past Alves.
Benzema skipped away from Ezequiel Garay before rattling the post from just inside the area, as Madrid stepped up their pursuit of a second goal just after half-time.
Parejo handed Madrid the chance to double the lead from the penalty spot when he was penalised for pulling back Luka Modric, but Alves guessed right and saved Ronaldo's effort low to his left.
3 - Diego Alves has saved the most penalties taken by Cristiano Ronaldo (three out of four). Martyrdom pic.twitter.com/q3iodDLtYD— OptaJose (@OptaJose) April 29, 2017
Madrid began to sit deeper to safeguard their slender advantage, although substitute Alvaro Morata wasted a good chance on the break, completely missing the ball after being teed up by Ronaldo.
The Portugal captain collided with the post 12 minutes from time, having somehow failed to turn the ball over the bar from point-blank range before clattering into the upright under pressure from youngster Toni Lato.
Valencia had done little to threaten Navas' goal, but they seemingly snatched a point in the 82nd minute, Parejo curling a brilliant free-kick into the top-left corner from 25 yards out to stun the Bernabeu.
However, four minutes later, Marcelo picked up a loose ball to the left of the Valencia box, cut inside onto his right foot and drilled low past Alves to salvage what could prove a critical win.
Key Opta stats:
- Cristiano Ronaldo has scored 367 goals in his career and is the top scorer in the top six European leagues, surpassing Jimmy Greaves (366).
- Ronaldo has netted 13 goals in his 15 games against Valencia in La Liga.
- Madrid have scored in their last 58 games in all competitions, the best run of a LaLiga team.
- Sergio Ramos has reached 400 games in La Liga: Real Madrid (361), Sevilla (39).
- Only Lionel Messi (8) has scored more goals from direct free-kicks than Parejo (7) in the last three La Liga seasons.