With 97 per cent of ballots counted across the Big Apple, the 52-year-old stood at 40.2 per cent of the vote, a hair's breadth above the magic number needed to avoid an October 1 run-off.
De Blasio's victory followed a bruising campaign overshadowed by the lurid sex scandal embroiling rival Anthony Weiner, who trailed in last.
However given the razor-thin margin, the city's former financial controller Bill Thompson in second place with 26.2 per cent, held out hope that De Blasio would drop below the 40 per cent mark and force a run-off.
New York is overwhelmingly Democratic, even though it has not elected a mayor from that party in 20 years.
More From This Section
The Democrat candidate will be seen as most likely to replace Michael Bloomberg, the billionaire mayor who has led the city for the past 12 years, after the November 5 mayoral election.
Republican candidate Joe Lhota scored a decisive victory with 52.6 per cent to businessman John Catsimatidis' 40.7 percent.
Observers have said the winning Democrat candidate may have a tough time against Lhota, former public transport chief who was endorsed by the New York Times in the mayoral election.
Former Democratic front runner, openly gay city council speaker Christine Quinn, suffered a blow to her campaign, coming in third with 15.3 per cent.
Disgraced former congressman Weiner, whose support was obliterated by revelations he sent sexually explicit messages to young women online, conceded defeat after landing finishing with 4.9 per cent of votes.
Former New York Governor Eliot Spitzer, another high-profile Democrat whose stellar political career went up in flames after a sex scandal lost to rival Scott Stringer in the primary race to become the city's financial controller.