Madrid's victory kept it six points behind Spanish leader Barcelona, but only just as it nearly blew a three-goal lead during a frantic finish set off after it surrendered a pair of second-half penalties.

Madrid coach Carlo Ancelotti will have no conundrum about selecting his attack for Tuesday's Champions League match at Juventus as Ronaldo, Gareth Bale, and Karim Benzema once again showed Madrid's potency up front.

Rayo defender Anaitz Arbilla was in tears after being substituted in the 51st minute, moments before Jonathan Viera scored a pair of spot kicks to turn the game with Madrid on the verge of conceding an equalizer at Vallecas Stadium right to the final whistle.

"To think that the game is over when you're winning 3-0 is a grave error," Ancelotti said. "It's happened twice now, and I am sure it will not happen for a third time."

Madrid's attack picked up from Wednesday's 7-3 win over Sevilla as Luka Modric sent Ronaldo racing down the left and skipping past his marker to move clear into the area and fire the third-minute opener past goalkeeper Ruben Martinez.

Bale picked out Benzema inside the area with a cross from the right to head home in the 31st for 2-0.

The winger showed cleverness when he flicked a ball over Arbilla to get free along the goal-line and he picked out Ronaldo streaking into the area to fire into the top of goal in the 48th.

Madrid was dominating the bottom table club until Pepe kicked out at Viera — whose inopportune touch earlier waived off a Iago Falque goal because of offside — and Marcelo flew into Alberto Bueno to set up Viera's goals in the 53rd and 55th.

"What's happened to us in these last two games is not just down to coincidence," Ancelotti said. "We've got to change our attitude and rid ourselves of this air of superiority we have on the field."

Diego Lopez certainly earned his reported salary boost as the Madrid goalkeeper got a tip to Bueno's smash for it to find the post, and he recovered to save from Lass Bangoura as his 65th-minute shot ricocheted through the area on goal.

Substitute Adrian Gonzalez nearly scored on his first touch, just rolling wide, as the hosts came close while goalkeeper Ruben Martinez denied Ronaldo late at the other end.

Sociedad dominated the visitors at its Anoeta Stadium with Ion Ansotegi, Inigo Martinez, and Antoine Griezmann all scoring through headers from set pieces, with the last coming after Osasuna defender Jordan Loties was shown a red card.

Haris Seferovic released Chori Castro to score in the 81st minute before capping the rout himself by scoring Sociedad's fifth with two minutes to play.

Sociedad goes into Tuesday's match against the English champions winless in the continental competition.

Beto gifted a pass to Alex Lopez to score an easy goal as the Portuguese 'keeper's glaring error allowed Celta Vigo to win 1-0 away at Sevilla, which has lost two straight.

Also, Rodrigo Rios scored his sixth goal with what TV replays showed was his hand as Almeria beat Valladolid, which played with 10-men over the final 15 minutes, 1-0.