Coach Carlo Ancelotti praised his side for refusing to give in even when they were playing poorly.
Valencia took the lead in the second half with a strike from Hugo Duro but Vinicius Jr levelled in the 86th minute before sending in a cross for Karim Benzema to head the visitors in front two minutes later.
Real also pulled off a last-gasp victory in the Champions League by beating Inter Milan with an 89th-minute goal from Rodrygo while they have now scored six goals in the final five minutes in five games in all competitions this season.
“I have a team that scraps and fights whether they are playing well or playing badly,” Ancelotti told reporters.
“We didn’t beat them thanks to our quality, we did it thanks to our indomitable spirit. We kept going right until the end.”
The win took Real to the top of the standings but Ancelotti believes his side are still far from their best.
“We have quite a young team and I’ll be the first person to admit we’re not playing spectacular football at the moment,” he said.
“We didn’t play well in the first half, we attacked well on the break but we were pegged back a lot and when you sit too deep it’s easy to concede goals.”
The defeat was hard to digest for Valencia coach Jose Bordalas, whose side overcame first-half injuries to Carlos Soler and Thierry Correia and had dominated the game until Real’s late fightback.
“We’re sad because the team made an incredible effort after the problems we had with Carlos and Correia,” he said.
“We did exactly what we wanted to do and played much better than them, pressing them high and controlling the game.
“We played 75 perfect minutes but we couldn’t afford to drop our concentration for even a second.
“We were a little unlucky but we should have managed the last few minutes better.
“We didn’t read the game well and ended up paying for our mistakes.”