Ramos on target
Ramos had initially seen his shot saved but was given a second attempt as a VAR review ruled that Leganes goalkeeper Juan Soriano had strayed off the goalline before diving to parry the ball away.
Real had made a flying start to the game, taking the lead in the seventh minute through 18-year-old Rodrygo Goes and doubling it 60sec later through Toni Kroos.
Madrid were awarded a second penalty in the second half but Ramos handed the ball to Karim Benzema and the Frenchman made no mistake to net the fourth goal.
Substitute Luka Jovic had a goal ruled out for off-side after a VAR review but the Serbian finally scored his first goal for the club since signing from Eintracht Frankfurt in June, completing the rout with a header in added time.
Earlier, Messi scored another spell-binding free-kick at Camp Nou after his sumptuous pass had already teed up Arturo Vidal for Barcelona's second. He then drove home his second late on before sliding through Luis Suarez to make it five.
Yet in truth his two goals and two assists were just the start of it, the defining moments of another mesmeric all-round performance that was dotted with flicks, passes and dribbles, not to mention two humiliating nutmegs.
Meanwhile, Atletico Madrid could have done with a drop of Messi's attacking inspiration as they were held to a disappointing draw by Alaves.
Alvaro Morata came off the bench to score his third goal in as many games, but Lucas Perez hit a stunning late equaliser at Menizorrotza to ensure a tight contest between two of La Liga's most stubborn defences ended all square.