MADRID -- Lionel Messi scored a hat trick to give Barcelona a 4-3 win at 10-man Real Madrid in an electrifying and contentious "clasico" match that kept the three-way Spanish league title race wide open.

The result means Atletico Madrid now tops the table, with Madrid level on points in second and Barcelona a point behind in third with nine games remaining as the title race shapes up to be the closest in years.

In a game that saw three penalties, a red card and a host of missed chances, Andres Iniesta set the tone by rocketing home an early opener, only for Karim Benzema's brace to put the hosts ahead by the 24th.

The teams went into the break level at 2-2 after Messi slotted in an equalizer, only for Cristiano Ronaldo to re-establish Madrid's lead with a 55th-minute penalty.

But Messi scored a pair of penalties after that — including the 65th-minute equalizer after Sergio Ramos was sent off — to end Madrid's 31-game unbeaten run stretching back to the last "clasico" in October.

The high pace of play and buzzing atmosphere inside the Santiago Bernabeu Stadium left little doubt this was a "clasico" with much at stake.

Messi spotted Iniesta's run down the left side seven minutes in and slid a perfect ball clear into the area for him to lift into the top of goal to kickstart the memorable match.

Madrid winger Angel Di Maria troubled right back Dani Alves throughout, especially when he twice set up Benzema with crosses from the left side, with the France striker nodding home the first in the 20th before controlling the second to smash a right-footed volley beyond Victor Valdes. The goalkeeper got a glove to Benzema's opener but couldn't keep it out.

The strengths and philosophies of each club were apparent as Barcelona looked to set the tempo through possession while Madrid always looked dangerous when breaking on the counter.

Madrid's defense nodded off when Neymar and Messi's hopeful combination came to fruition and the Argentina star could equalize before halftime, with Benzema left ruing a number of missed chances that could have given Madrid control.

Gareth Bale's speed provided problems for Barcelona down the right, but Ronaldo was equally troublesome along the left and he was tripped by Dani Alves after the break to set up the penalty for his league-leading 26th goal that put Madrid back on top. However, eplays showed the contact had taken place just outside the penalty area.

But Barcelona replied when Ramos was shown a straight red for tangling with Neymar and Messi stepped to equalize again.

Benzema then came off as Madrid looked to defend the draw, with Barcelona patiently coming forward and Dani Alves sending a shot off the post in the 74th. Iniesta was then taken down by Xabi Alonso and Dani Carvajal in the area to set up the 84th-minute spot kick that Messi slotted home for his 21st goal of the season.

Messi climbed to No. 2 on the Spanish league's all-time scoring list with 235 goals, passing Hugo Sanchez. He also became the leading scorer in "clasico" matches with 21 goals, bettering the mark of 18 he shared withMadrid great Alfredo di Stefano.