BARCELONA, Spain — Cristiano Ronaldo got the better of Lionel Messi on Tuesday, scoring twice to lead Real Madrid to a 3-1 win at Barcelona and a spot in the Copa del Rey final.

After the first leg of their semifinal ended 1-1, Madrid neutralized Barcelona’s passing game and counterattacked to perfection at the Camp Nou to hand its archrival its first home loss in any competition since last April.

It was also a second straight cup setback for the Catalan club, which is running away with the Spanish league but lost 2-0 at AC Milan in the first leg of its Champions League last-16 matchup.

While Messi had a quiet night, Ronaldo scored for a sixth straight visit to Camp Nou when he earned and converted a penalty in the 13th minute. Ronaldo hit again on the break in the 57th before Raphael Varane got his second goal of the series in the 68th.

Jordi Alba pulled a late goal back for defending champion Barcelona, but it was much too late to prevent Madrid from going through 4-2 on aggregate.

Madrid will play either Atletico Madrid or Sevilla in the cup final on May 18.

For Barcelona, it was their worst defeat to Madrid since losing 4-1 at the Santiago Bernabeu in 2008.

Barcelona suffered the same problems it had against Milan, unable to convert its better share of possession into goals, while its defense again proved brittle as it allowed goals for the 12th straight match.

Barcelona was again without coach Tito Vilanova, who is still undergoing treatment in New York after having a second tumor in two years removed from his throat in December.

Barcelona, which was again led by assistant coach Jordi Roura, has five wins, three draws and three losses since mid-January.

It faces Madrid again on Saturday in another “clasico,” that time away in the league. Barcelona already has a firm grip of that title, with a 12-point lead atop the standings and with defending champion Madrid even further back.