With 97 percent of ballots counted across the Big Apple, the 52-year-old stood at 40.2 percent 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 percent, held out hope that De Blasio would drop below the 40 percent mark and force a run-off.
"We are going to wait for every voice to be heard, every voice to be counted. This is far from over," said Thompson, the sole African-American candidate who narrowly lost to Bloomberg in 2009.
New York is overwhelmingly Democratic, even though it has not elected a mayor from that party in 20 years.
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 percent to businessman John Catsimatidis' 40.7 percent.
"We have a proud tradition in this city of electing strong officials, regardless of party affiliation...I know what it takes to get the job done," Lhota said in his victory speech.
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 frontrunner, openly gay city council speaker Christine Quinn, suffered a blow to her campaign, coming in third with 15.3 percent.
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 percent of votes.
"We had the best ideas, sadly I was an imperfect messenger," said Weiner.
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.
Amid much soul-searching over Bloomberg's legacy, many remained torn until the last minute over who to vote for.
"I really vacillated until the last minute," said Rosemary Wakeman, a professor at Fordham University, after voting in midtown Manhattan.
In the end, Wakeman went with De Blasio, the most left-leaning of the candidates, who has firmly led polls since mid-August on an anti-Bloomberg platform, accusing the mayor of overseeing a widening gap between rich and poor.
"I became less supportive of Bloomberg's policies," said Wakeman.
The city's six-foot-five, graying public advocate, De Blasio has hinged his campaign on inequality, describing New York as "a tale of two cities."
He has also campaigned hard against stop-and-frisk -- a police tactic loathed by minority communities who see themselves as unfairly targeted.
De Blasio's boldest proposal is a tax on those who earn over $500,000 to pay for after-school care for young children.
In a strange twist polls showed him scoring more support among black voters than Thompson.
De Blasio has featured his black wife Chirlane -- a former outspoken lesbian -- and bi-racial son Dante strongly in his campaign.
Earlier this week he hit out at comments made by Bloomberg to New York magazine that he had run a "class warfare and racist" campaign.
Bloomberg, who scored a third term after cajoling City Council into lifting a two-term limit in 2009, has presided over a falling murder rate and sweeping public health reforms.
However he has been accused of governing for the rich, a charge he strongly rejects.