championship since 2008, forcing Barcelona to relinquish the title after three straight years despite winning 4-1 against Malaga in which Messi scored three goals for the ninth time this season.
Madrid's players celebrated wildly after the final whistle at San Mames stadium, with coach Jose Mourinho displaying seven fingers to symbolize the number of league titles he has won in his career, including at FC Porto, Chelsea and Inter Milan.
Madrid has a seven-point lead over Barcelona with two games left.
Gonzalo Higuain scored for Madrid in the 16th minute and Mesut Oezil doubled the lead four minutes later. Cristiano Ronaldo added his 58th goal of the season and a record 115th for his club this campaign in the 50th, making up for a penalty in the 12th that was saved by goalkeeper Gorka Iraizoz.
Messi broke the record of 67 goals Gerd Mueller set for Bayern Munich in 1972-73. He converted penalties in the 35th and 59th minutes and chipped goalkeeper Carlos Kameni in the 64th for his seventh league hat trick, tying the mark set by Ronaldo this season.
The 24-year-old Messi has played 57 games for Barcelona this season, and broke records by scoring 46 goals in the league and 14 in the Champions League.
But while Barcelona celebrated Messi's scoring feats, Madrid's famous Plaza de Cibeles began filling with fans in anticipation of Madrid celebrating the end of its great rival's league dominance and yet another title for Mourinho.
Ronaldo also missed a penalty in the Champions League semifinal shootout loss to Bayern Munich last week, and his weak spot kick on Wednesday was easily stopped by Iraizoz to give supporters some early concern.
But Higuain's impressive goal — his 22nd this season — put Madrid on course for victory and Oezil made it 2-0 soon after. Ronaldo made amends for his miss with a header.
At the Camp Nou, Carles Puyol's 13th-minute opener for Barcelona was canceled out by Jose Rondon for Malaga.
Iniesta was fouled inside the area to set up Messi's 34th-minute spot kick that matched the 66-goal tallies by Pele for Brazilian club Santos in 1958 and Ferenc Deak for Hungarian team Ferencvaros in 1948-49.
Malaga midfielder Duda barged into Messi to set up his second successful penalty and match Mueller. Messi surpassed the former West Germany striker when Iniesta slipped a perfect long ball between defenders.
Earlier, Zaragoza beat Levante 1-0 to keep its hopes alive of avoiding relegation following Rayo Vallecano's 1-0 loss at Mallorca, which scored through Chori Castro in the 62nd minute.
Edu Oriol's 11th-minute goal proved the difference for Zaragoza, which moved within three points of Rayo with two games left.
Rayo has 40 points, Zaragoza 37 and Sporting Gijon 34. Racing Santander is already relegated and will finish last.
Also, substitute Carlos Vela's stoppage-time goal gave Real Sociedad a 1-1 draw at 10-man Atletico Madrid to all but end Atletico's hopes of qualifying for next season's Champions League.
Valencia thrashed Osasuna 4-0 to move three points ahead of Malaga and keep a firm grip on third place and an automatic Champions League qualifying place.
Levante is three points behind Malaga, which is fourth and in the final Champions League qualifying spot. Atletico trails Malaga by five points.
Real Betis beat city rival Sevilla 2-1.