LAS VEGAS — Canelo Alvarez and Gennady Golovkin fought 24 rounds in the space of a year, with little to pick between them.

When the scorecards were totaled Saturday night, though, the judges crowned a new middleweight champion of the world – but just barely.

Alvarez won the 160-pound titles held by Golovkin by the narrowest of margins, taking a majority decision to hand the longtime champion his first loss as a pro in a spirited fight that had a sold out crowd roaring.

Two judges gave Alvarez the final round, allowing him to pull out the win.

A year after the two fought to a draw, the second fight was almost as close. There were no knockdowns, but the action was spirited throughout as the two battled to the final bell before a frenzied crowd at the T-Mobile Arena.

Two judges favored Alvarez 115-113, while a third had it 114-114. The AP scored it 114-114.

“He’s a great fighter but I’m a great fighter and I showed it tonight,” Alvarez said.

The two fighters switched roles from their first fight, with Triple G trying to counter Alvarez and the Mexican fighter coming forward much of the fight. Both landed well to the head and Golovkin (38-1-1) controlled some rounds with his jab though neither were ever in any trouble of going down.

The fight was a rematch of a draw last September that left neither fighter satisfied. This time it was Golovkin who was upset, and he stormed out of the ring without talking.

“I’m not going to say who won tonight because the victory belongs to Canelo according to the judges,” Golovkin said later from his dressing room. “I thought it was a very good fight for the fans and very exciting. I thought I fought better than he did.”

Ringside punch stats showed a close fight, though they favored Golovkin by a small margin. Golovkin was credited with landing 234 of 879 punches while Alvarez (50-1-2) landed 203 of 622.

“We had a great fight, the one we expected the first time around,” said Abel Sanchez, Golovkin’s trainer. “I had it close going into the 12th round. We had good judges who saw it from different angles – I can’t complain about the decision, but it’s close enough to warrant a third fight.”

Almost immediately there was talk of a third fight between two middleweights who now know each other well.

“If the people want us to do it again let’s do it again,” Alvarez said. “For now I’m going to enjoy it with my family.”

“Under the right conditions, yes,” Golovkin said of a rematch.

Alvarez seemed to take control of the fight in the middle rounds, using sharp punches to batter Golovkin, who seemed to slow down from his pace earlier in the fight.

“We’re losing the fight!” Sanchez told Golovkin after the eighth round.

But Triple G came on strong in the final few rounds to make it as close as it could be. Golovkin landed several big punches to start the 12th round but still lost it on the two scorecards that ended up favoring Alvarez.

Both fighters were cut with Alvarez having one over his left eye and Golovkin cut over the right eye.

It was the first loss in 40 fights for Golovkin, the fearsome puncher from Kazakhstan who held portions of the middleweight title for seven years and hadn’t lost since the 2005 amateur world championships. And it came at the hands of the red-headed Alvarez, a Mexican star whose positive test for clenbuterol forced the rematch to be postponed from May.

They put on another show before a roaring crowd of 21,965, who crowded into the arena on the Las Vegas Strip with high anticipation in the biggest fight of the year.

Most of the crowd on Mexican Independence Day weekend favored Alvarez, who seemed to control much of the pace of the fight even while taking some sharp shots to the head. Though Golovkin has a reputation as a knockout artist, he never seemed to hurt Alvarez, who credited his fight plan with the win.

“I showed my victory with facts,” Alvarez said. “He was the one who was backing up. It was a clear victory.”

Alvarez said in the days leading to the fight that he went to bed every night visualizing a knockout. He seemed to want to follow up on his prediction, stalking Triple G at times, but never seemed to really hurt him.

Alvarez was guaranteed $5 million to $4 million for Golovkin, though both fighters were expected to make many millions more from the biggest pay-per-view in boxing so far this year.