In a victory for the Democratic party's progressive wing, Mamdani defeated former governor Andrew Cuomo and Republican Curtis Sliwa.
Mamdani must now navigate the unending demands of America's biggest city and deliver on ambitious - sceptics say unrealistic - campaign promises.
With his commanding win on Tuesday, the democratic socialist made history as the city's first Muslim mayor, the first of South Asian heritage and the first born in Africa.
He will also become New York's youngest mayor in more than a century when he takes office in January.
"The conventional wisdom would tell you that I am far from the perfect candidate. I am young, despite my best efforts to grow older. I am Muslim. I am a democratic socialist. And most damning of all, I refuse to apologise for any of this," Mamdani declared to a roaring crowd at his victory party.
More than two million New Yorkers cast ballots, the largest turnout in a mayoral race in more than 50 years, according to the city's Board of Elections.
With roughly 90 per cent of the votes counted, Mamdani held a nine percentage-point lead over Cuomo.
Mamdani's unlikely rise gives credence to Democrats who have urged the party to embrace more progressive candidates in hopes of winning back swing voters who have abandoned the party.
He has already faced scrutiny from national Republicans, including President Donald Trump, who have eagerly cast him as a threat and the face of a more radical Democratic Party that is out of step with mainstream America.
Trump has repeatedly threatened to cut federal funding to the city - and even take it over - if Mamdani won.
In his concession speech, a defiant Cuomo called his campaign "a caution flag that we are headed down a dangerous, dangerous road" and noted that "almost half of New Yorkers did not vote to support a government agenda that makes promises that we know cannot be met".
Mamdani's grassroots campaign centred on affordability, and his charisma spoiled Cuomo's attempted political comeback.
The former governor, who resigned four years ago following allegations of sexual harassment that he continues to deny, was dogged by his past throughout the race and was criticised for running a negative campaign.
Mamdani will also have to deal with Trump, who not only threatened retribution against the city but also suggested he might try to arrest and deport Mamdani if he won.
Mamdani was born in Uganda but was raised in New York City and became a US citizen in 2018.
In his speech, Mamdani addressed Trump head on.
"New York will remain a city of immigrants, a city built by immigrants, powered by immigrants and as of tonight, led by an immigrant," he said, adding that, "If anyone can show a nation betrayed by Donald Trump how to defeat him, it is the city that gave rise to him."
Trump appeared to acknowledge Mamdani's challenges, posting "…AND SO IT BEGINS!" on his Truth Social site.
Mamdani now has to implement his ambitious but polarising agenda.
Among his promises are free child care, free city buses, city-run grocery stores and a new community safety agency that would send mental health workers to handle certain emergency calls rather than police officers.
It is unclear how Mamdani will pay for such initiatives, given Democratic Governor Kathy Hochul's steadfast opposition to his calls to raise taxes on wealthy people.