New York City Democratic Mayoral hopeful Bill De Blasio embraces his son Dante, left, daughter Chiara, second from left, and wife Chirlane, right, after addressing supporters at his election headquarters after polls closed in the city's primary election Wednesday, Sept. 11, 2013, in New York. (Kathy Willens/AP)

Democratic voters here delivered a resounding rebuke to Mayor Michael R. Bloomberg Tuesday, choosing from a crowded primary field his sharpest and most liberal critic as their nominee for mayor.

Early results showed Public Advocate Bill de Blasio with a substantial lead over his closest rivals in what had been a chaotic, surreal, sometimes X-rated primary contest, but it was still not clear whether he had won enough of the vote to avoid an Oct. 1 runoff against the second-place finisher, former city comptroller William Thompson. In the end, the race to succeed the billionaire mayor after three terms turned on whether the city wanted to build on or steer away from the substantial Bloomberg legacy, and de Blasio suggested the sharpest break.

The city underwent an existential transformation under Bloomberg.

The vice-ridden Gotham of public imagination reached unprecedented levels of safety as crime plummeted even further than it had under Rudolph W. Giuliani, although without the same level of racial tension. The waterfronts that once blighted the perimeters of Manhattan and Brooklyn bloomed with development. Bike lanes and picnic tables sprouted in intersections once crowded with cars.

On the flip side, the policing tactics that supporters say helped bring down crime, stopping and frisking people in high-crime areas, were ultimately deemed to be racial profiling. Also, there were forgotten pockets of the restoration, and the resulting economic inequality served as de Blasio’s most potent talking point.

Bloomberg’s dreamscape became the Democrats’ dystopia. In their telling, he was not a refreshing, fearless leader but an overbearing father who thought he always knew best.

“The mayor,” de Blasio said Tuesday, “has been increasingly unwilling to address inequality in this city and that this is the central issue of our times.”

De Blasio, who has close ties to the city’s unions, surged late in the contest with an unabashedly progressive, anti-Bloomberg message and deft public deployment of his interracial family that seemed to sweep the city’s Democratic base off its feet. The only real suspense on Election Day centered on whether de Blasio could reach the 40 percent threshold to avoid a runoff. Falling short would mean having to face the mild-mannered Thompson, the only African American candidate in the race. With 96 percent of the vote counted, de Blasio was just above 40 percent, to Thompson’s 26 percent.

“To the people of New York, I say thank you,” de Blasio said after midnight, onstage at his packed victory party in Brooklyn. He added that he would take his “unapologetically progressive  alternative” to Bloomberg’s policies to the “next round” of the campaign.

Former congressman Anthony Weiner (D.N.Y.), who had engineered a surprising comeback from a sexting scandal, provided some of the most bizarre moments of the campaign. The once-promising Weiner candidacy devolved into a national joke upon the revelation that he had created a frisky alter-ego, Carlos Danger, to continue his sexually explicit social-media engagement, even after being forced to resign his congressional seat because of the initial sexting scandal. Weiner was relegated to the role of potential spoiler for de Blasio, since his single-digit support had the potential of preventing the front-runner from reaching the critical 40 percent threshold. Beyond that, Weiner, who once eclipsed the field, was essentially absent from the race.

Instead Sydney Leathers, the woman with whom he had explicit exchanges, showed up in a tight dress in front of his headquarters, saying she was “not at all” worried about disappearing from the public spotlight when his candidacy does. The Weiner campaign subsequently moved a scheduled appearance to another borough.

Weiner was getting just under 5 percent of the vote in the final tallies Tuesday, and another disgraced politician seeking redemption also came up short. Former governor Eliot Spitzer lost his primary bid in the race for city comptroller.

In the run-up to the vote, Thompson and City Council Speaker Christine Quinn both sought to slow de Blasio’s march to victory with attacks that accused him of accepting donations from slumlords and pandering to voters with proposals such as universal kindergarten, paid for by tax increases on the rich.

None of those attacks seemed to dampen de Blasio’s mood on Election Day. More than three dozen cameramen and reporters awaited the candidate and his wife, Chirlane McCray, in front of a library in the Park Slope section of Brooklyn.

The cameras clicked when de Blasio, in suit and yellow tie, arrived holding his wife’s hand. The imagery of the de Blasio family has been key to his success. McCray, an African American former lesbian, has appeared prominently in his ads. So have the couple’s teenage children: Chiara, who once introduced him as “not a boring white guy” and Dante, whose monumental Afro has hovered over the entire campaign.

De Blasio attributed his success to voters paying attention and getting serious about the direction of the city. He accused the mayor of being “out of touch with the people he is supposed to be leading.”

That is a message that has reverberated throughout the Democratic primary. The mayor sent de Blasio something of a parting gift last week, telling New York magazine that the campaign ads prominently featuring de Blasio’s family were “racist.”

“People all over the city have come up to express their dismay and confusion – why did he say that?” said de Blasio as he stood next to his wife. He quickly sought to turn the slight to his advantage. “I really want to thank Governor Cuomo for stepping up and saying, both as our family friend as well as the leader or our state and our party, that those comments were inappropriate, and I appreciate Governor Cuomo’s vote of confidence.”

Thompson, who narrowly lost to Bloomberg in 2009, spent the morning resting after a 24-hour get-out-the-vote effort that had him visiting Baptist ministers, bakeries and candlelight vigils against gun violence through dawn. As he slept, ads around the city promoted him as the “first African American comptroller in the history of our city” and a friend to President Obama.

Quinn was also out scrambling. Before she arrived late to her lucky campaign corner in Chelsea, a woman handing out pamphlets that promoted her major newspaper endorsements called “Good morning, women! Christine Quinn, first woman for mayor!” A man in horn-rimmed glasses, blue blazer and calf-high riding boots walked by Tim Gunn, the fashion celebrity who waited in a natty plaid suit under a “Tim Gunn #In With Quinn” sign. He observed an “aesthetic divide” between those supporting Quinn (“well groomed, good looking”) and those not (“schlumped over and drooling.”)

Quinn described her historic status as the first openly gay candidate as “an enormous step forward for our community and something I’m very proud to have been able to be a part of.” She also embraced her work with Bloomberg: “I’m proud of the things I’ve been able to accomplish for this city working with my colleagues and working with the mayor, and I’m also proud of the times I stood up to the mayor.”

But early exit polls showed that de Blasio had greater support among blacks than Thompson and more backing among gays than Quinn.

The break with Bloomberg that served de Blasio so well during the Democratic primary could come back to haunt him in the general election. New Yorkers have not elected a Democrat to be mayor in more than 20 years, and de Blasio got his professional start in the often-disparaged administration of David Dinkins, the last Democrat to win the job.

“We’ve had five terms of anything but Democratic control of the mayor’s office,” Joe Lhota, who won the Republican primary Tuesday, said in a phone interview. “There’s no doubt that candidates on the Democratic side have shifted consistently to the left and, I believe, to their long-term detriment.”