Bill De Blasio, the liberal pragmatist who sprang from nowhere to storm the Democratic race for New York mayor, was within a hair's breadth of winning his party's nomination outright on Wednesday.

Unofficial tallies put De Blasio at 40.2%, just over the 40% needed to avoid a run-off on 1 October. A decisive result would place De Blasio in the front-runner position for the November general election, where he would go up against the former chief of New York's transport agency, Joe Lhota, who won the Republican nomination.

But official results put him just under 40% and should a final count force De Blasio to go to a second round, he would go face Bill Thompson, the city's former comptroller, or chief auditor, who took second place with 26%. Christine Quinn, the city council leader and early favourite, was beaten into a distant third place.

In his election night speech, delivered close to midnight, Thompson made it clear that he would not go easily. He said that he would ensure that "every vote counted" – a strong hint that he intends to wait for an official declaration that included absentee ballots, which may not come for three weeks.

In exit polls conducted by Edison Research, De Blasio trounced his rivals among all main demographic groups. He held sway among men (44%) and women (42%), whites (46%), blacks (44%) and Hispanics (41%), all the city's main religious communities and all age groups.

The result was a resounding vindication for De Blasio's unconventional approach to the New York mayoral race in which he cut across traditional racial and ethnic lines to build what the Guardian analyst Harry Enten called "the most diverse coalition in modern history". Key to his success, propelled from a little-known fourth place contender just a few weeks ago to his party's front-runner, was his message of a "tale of two cities" – an implicit attack on the style of leadership of the current mayor, billionaire Michael Bloomberg.

In his victory speech, delivered in his home neighbourhood of Park Slope, Brooklyn, De Blasio returned to the theme tune of his campaign, promising an alternative to the Bloomberg era. New York, he said, had become "a tale of two cities – one where the very wealthy had not only rebounded from the great recession but life couldn't get much better for them, and another New York where nearly half are living on or near the poverty line, where luxury condos replace community hospitals, where pro-active policing has slipped into racial profiling."

De Blasio, 52, the city's current public advocate, used a vociferous assault on the "stop and frisk" policy promoted by Bloomberg and his police chief, Ray Kelly, to position himself as the liberal choice and to force the previous Democratic front-runner, Christine Quinn, to defend herself against accusations she was too close to the incumbent mayor.

The devastating impact of De Blasio's strategy was underlined by exit polls: Democratic primary voters who think "stop and frisk" is excessive swung behind him by a whopping 56%, while Quinn was backd by only 7% of black voters, who are overwhelmingly on the receiving end of the aggressive policing policy.

For Quinn the night was bitter. The speaker of the city council was bidding to be New York's first woman and first gay mayor, yet she only managed to attract 15% of the vote, having tumbled from her leading position earlier in the summer.

Anthony Weiner, another toppled front-runner, also had a punishing night, though 5% of Democratic voters stuck with him despite his highly publicised recidivist problems with internet flirting. In a tear-filled concession speech, in which his wife Huma Abedin was notable by her absence, he said: "We had the best ideas. Sadly I was an imperfect messenger." He left in a flurry, and was pictured giving a one-fingered gesture of insult to reporters as he was driven away.

As deliberations continued about whether De Blasio would cross the crucial 40% mark to avoid a run-off, a much more clear picture was given in the Republican primary. By 11pm Lhota, the former chairman of the Metropolitan Transportation Authority, which runs the New York subway, was declared the outright winner with 52% of the vote against 41% for the billionaire businessman John Catsimatidis.

Though most of the attention so far in the mayoral race has focused on the battle for Democratic nomination – a reflection of the overwhelmingly Democrat-leaning electorate in the city – the Republican candidate should not be written off. Lhota is progressive on many social issues including gay marriage, abortion and marijuana legalisation.

New York also has a tradition of backing Republican mayors, including Bloomberg and Rudy Giuliani. The last Democratic mayor in the city was David Dinkins in 1993.

Lhota lost no time in throwing down the gauntlet to De Blasio, accusing the Democratic favourite of "class warfare" and of wanting to divide the city. "We may be five boroughs but we are one people, one city. The last thing we want is to send our people back to the age of fear and hopelessness."

Earlier in the day, the city that is vying to become the East Coast rival to Silicon Valley gave a dinosaur's display of voter technology when it struggled to make 50-year-old lever machines work at polling stations. Having concluded that the modern electronic voting system introduced at a cost of $95m in 2010 was too slow to cope with a possible run-off, the Board of Elections opted to go back to the future by dragging out 5,100 lever machines that date from the 1960s.

There were reports of failing lever machines in polling stations across the city. To add to the awkwardness two of the reports concerned mayoral candidates: Lhota had to resign himself to voting by paper in Brooklyn Heights after his lever machine wouldn't function and Weiner ran into problems in Manhattan when polling officials couldn't find his signature on the voter rolls.

The night also came to a disappointing end for Eliot Spitzer, the former governor of New York state running for city comptroller in an attempted political comeback following a prostitution scandal by. He lost to the current Manhattan borough president Scott Stringer by 48% to 52%.