Raul de Tomas, on loan from Real Madrid, had given Rayo a surprise lead with an excellent 20-yard strike.
Gerard Pique headed an equaliser from Messi’s free-kick and the Barca captain nonchalantly put the treble-chasing champions ahead from the spot.
Luis Suarez tapped home a late third after a one-two with Ivan Rakitic.
Messi is La Liga’s top scorer with 26 goals in 25 games, with Suarez second on 17.
Second-placed Atletico Madrid had briefly narrowed the gap on Barca with a 1-0 win over Leganes earlier on Saturday, Saul scoring the only goal.
Barcelona were made to work hard by Rayo Vallecano, who are in the relegation zone after six consecutive defeats, but Ernesto Valverde’s side are now unbeaten in 14 La Liga games, a run going back to November. They have won 13 consecutive league games against Rayo since 2002.
Next up for the Catalan side is a home game against Lyon in the Champions League last 16 on Wednesday, having drawn the first leg 0-0 in France.
- 1ter Stegen
- 2Nélson Semedo
- 5BusquetsBooked at 79minsSubstituted forMalcomat 85′minutes
- 8Ramos de Oliveira MeloSubstituted forDembéléat 45′minutes
- 7CoutinhoSubstituted forRakiticat 80′minutes
- 20VelázquezBooked at 68mins
- 16AmatSubstituted forPozoat 64′minutes
- 7Moreno Lopera
- 14Dias Correia
- 12ImbulaBooked at 89mins
- 18GarcíaSubstituted forEmbarbaat 80′minutes
- 9de Tomás Gómez
- 2Román Triguero
- 25Di Santo
- Mario Melero López
- Shots on Target
Giannelli Imbula (Rayo Vallecano) is shown the yellow card.
Foul by Giannelli Imbula (Rayo Vallecano).
Attempt missed. Raúl de Tomás (Rayo Vallecano) right footed shot from more than 35 yards is too high. Assisted by Giannelli Imbula.
Raúl de Tomás (Rayo Vallecano) wins a free kick in the defensive half.
Foul by Alejandro Gálvez (Rayo Vallecano).
Attempt blocked. Bebé (Rayo Vallecano) right footed shot from outside the box is blocked. Assisted by Adrián Embarba.
Substitution, Rayo Vallecano. Adrián Embarba replaces Álvaro García.
Giannelli Imbula (Rayo Vallecano) wins a free kick in the attacking half.
Corner, Rayo Vallecano. Conceded by Jordi Alba.
Attempt blocked. Raúl de Tomás (Rayo Vallecano) right footed shot from the right side of the box is blocked.
Attempt blocked. Emiliano Velázquez (Rayo Vallecano) header from the centre of the box is blocked. Assisted by Giannelli Imbula with a cross.
Corner, Rayo Vallecano. Conceded by Nélson Semedo.
Emiliano Velázquez (Rayo Vallecano) is shown the yellow card for a bad foul.
Foul by Emiliano Velázquez (Rayo Vallecano).
Offside, Rayo Vallecano. José Pozo tries a through ball, but Santiago Comesaña is caught offside.
Attempt missed. José Pozo (Rayo Vallecano) left footed shot from the centre of the box misses to the right following a corner.
Corner, Rayo Vallecano. Conceded by Samuel Umtiti.
Attempt blocked. Raúl de Tomás (Rayo Vallecano) right footed shot from outside the box is blocked. Assisted by Santiago Comesaña.
José Pozo (Rayo Vallecano) wins a free kick in the defensive half.
Substitution, Rayo Vallecano. José Pozo replaces Jordi Amat.
Foul by Bebé (Rayo Vallecano).