(Click here for video of Messi's goals.)
Nikola Zigic's penalty put Racing ahead in the 56th minute, but Messi won the game with a pair of goals that increased his season total to 16 goals.
"You always know that Messi is capable of creating a goal and fortunately for us he created two," Barcelona coach Pep Guardiola said. "We had dominated but without creating much going forward against a well-organized Racing team."
Barcelona had Mexican Rafael Marquez and Gerard Pique sent off late for bad fouls.
Marquez's appearance and red card was good news for Mexico. He'll be suspended from next weekend's game, allowing him to be rested for Mexico's Hexagonal opener against the USA on Feb. 11. He had been injured and was expected to be out for at least another week, putting his availability for the Columbus game in doubt.
Real beat Numancia, 2-0. Captain Raul scored his 307th goal for the club to equal Alfredo di Stefano's all-time club goalscoring record.
Atletico Madrid's troubles continued as it lost, 2-1, at home to Valladolid on Sunday. The defeat again raised speculation that Mexican coach Javier Aguirre would be fired.
"We have hit rock bottom in terms of play and results," admitted Aguirre. "We can't do any worse."