The Dutchman restored Real's lead from the penalty spot but Messi levelled again when he smashed high into the net on 29 minutes.
Barca suffered a major blow when Oleguer Presas was sent off before the break and they spent most of the second half defending as Real tried to take advantage of their lead.
Real defender Sergio Ramos looked to have secured victory when he flicked in a header 17 minutes from time but Messi completed a virtuoso performance with his late goal.
Messi said: "I was given a lot of space by my teammates. I tried to take advantage and today it worked out. It was a very special match.
Real Madrid striker Ruud van Nistelrooy
scored twice in a frantic first half hour [Reuters]
"Madrid benefitted from having an extra player. If it hadn't, the game would have been very different."
Real's performance likely will ease criticism of coach Fabio Capello, whose tenure came under scrutiny after Madrid were eliminated from the Champions League by Bayern Munich on Wednesday.
Barcelona climbed to the top of the Spanish Primera Liga table on goal difference, but Sevilla can reclaim the lead when they play Gimnastic Tarragona on Sunday.