Miguel Cotto’s planned valedictory lap to close a decorated 17-year professional career ended in surprise on Saturday when he suffered a unanimous-decision loss to Brooklyn’s Sadam Ali at Madison Square Garden.

So distant an afterthought was Ali entering Saturday’s fight that he scarcely warranted a mention on the poster advertising the bout. But the 29-year-old son of Yemeni immigrants from Brooklyn hurt the crowd favorite in the second and fourth rounds, weathered Cotto’s best onslaughts in the middle act and held on to capture the Puerto Rican icon’s WBO junior middleweight title before a stunned audience of 12,391.

What had been sold as a celebration of one of boxing’s classiest and most understated champions instead became an unexpected coronation as Ali, a 2008 US Olympian who moved up from welterweight to challenge for Cotto’s title, scored a career-best win over the first and only Puerto Rican man to win belts in four weight divisions – and offered up yet another reminder that for all Cotto’s accolades boxing doesn’t hand out gold watches.

“I worked hard for it,” said Ali, who won by scores of 116-112, 115-113 and 115-113. “I took advantage of this fight, and I made sure to make it count.”

Ali (26-1, 14 KOs) had been largely outclassed in his lone previous title shot, a ninth-round TKO loss to Jessie Vargas for a vacant welterweight strap last year, leaving many pessimistic about his chances on Saturday against one of the finest champions of his era and the third-biggest star after Floyd Mayweather and Manny Pacquiao. Not least the oddsmakers, who installed him as a 6-1 underdog.

But Cotto (41-6, 33 KOs) struggled with Ali’s hand speed, looking every bit of his 37 years as he absorbed a series of big shots in the early rounds, including a right hand to the temple in the second that staggered him and a left hook in the fourth that sent him reeling.

Facebook Twitter Pinterest Ali lands a punch on Cotto during Saturday’s fight. Photograph: Al Bello/Getty Images

Cotto, fighting in his 10th main event at the hallowed Manhattan ground that’s become his adopted home, rallied back valiantly in the middle rounds, mostly behind an educated jab that kept his opponent tentative. A straight right hand in the sixth rocked Ali and sent the whipped the partisan Puerto Rican crowd into a frenzy. But Cotto’s refusal or inability to stick with the jab proved crucial as Ali’s confidence redoubled as the fight moved into the later stages.

Still, Ali trailed on two judges’ scorecards entering the ninth and was even on the third’s, but he was the busier fighter down the stretch and closed with brio, scoring an upset that will no doubt change the trajectory of his career.

“I had him hurt here or there in the first couple of rounds,” said Ali, who connected with 139 of 647 punches (21%) compared to 163 of 536 (30%) for Cotto. “I knew I had to do something, or he would have dug in. By the 11th, I thought the fight was close.”

Afterward, Cotto acknowledged that he’d injured his left bicep in the seventh round, but refused to attribute his sixth career loss to the injury and reiterated his intent to walk away.

“Feeling good,” Cotto said. “Feeling good with the performance. Something happened to my left bicep, seventh round. I don’t want to make excuses, Sadam won the fight. It is my last fight. I am good and I want to be happy in my home with my family.”

For Ali, who earned $600,000 for Saturday’s work compared to $750,000 for Cotto, it was a career-altering night. What comes next is unclear due to the network alliances that continue to hamstring this once-mainstream sport, but it was the last of the Brooklynite’s concerns in the afterglow of unexpected triumph.

“Whatever [Golden Boy Promotions] has next, I’ll take it,” Ali said. “Good things happen to good people. I have been training since I was eight years old, and I am glad I got this win at MSG in my hometown.”