For months, New York City Public Advocate Bill de Blasio was little more than an afterthought in the Democratic primary of the city’s 2013 mayoral race, which was City Council Speaker Christine Quinn’s to lose up until she did just that Tuesday night, concluding her spectacular fall from favor.

As of three months ago, Quinn had won every single primary poll conducted in 2013, while most New Yorkers probably remained unable to pick de Blasio out of a crowd.

But over the past few weeks, de Blasio, a physically imposing Brooklynite many have described as the antithesis of Mayor Michael Bloomberg, shot to the front of the pack in an unexpected surge that left news outlets scrambling to profile a man most people had counted out of the race long before it even began.

In fact, it looks increasingly likely that de Blasio won a full 40 percent of the city vote on Tuesday, which would mean there is no need for a runoff. If that’s the case -- unless the near-impossible happens and Republican challenger Joe Lhota can pull off a massive upset -- de Blasio would become mayor in January after the formality of the general election.

The question that remains is why on earth and how in the world did this happen? How did a boring Brooklyn father with next-to-no name recognition weather Anthony Weiner-mania and leapfrog not only Quinn but also former City Comptroller Bill Thompson, who came in second place yet again on Tuesday (Thompson lost to Bloomberg by a slim margin four years ago.)

A left-leaning Democrat, de Blasio started out in city politics by working for David Dinkins’ successful 1989 campaign for mayor, but he emerged on the national political scene in 2000, when he was tapped to run Hillary Clinton’ successful Senate campaign.

And four years ago he quietly ascended to the office of public advocate, a position many New Yorkers don’t realize exists, but which is officially (but probably not actually) the second-most-powerful elected citywide position. It’s a strange post, in that a public advocate is supposed to answer to and represent the people, as a check to the mayor’s power, but de Blasio took to it with gusto, turning it into a lofty platform from which to make his views known as the campaign heated up.

He made his presence felt everywhere over the past few months, from a Trayvon Martin rally in Union Square to a fundraiser at a Flatiron ping-pong club hosted by actress Susan Sarandon, and as public advocate, he was able to speak both as an elected official and a candidate.

From there, he established himself as a strong voice against stop and frisk, slowly introducing voters to his African-American wife and their son in the process. New York politics always have a racial component, and this year was no different, as the impact of his views on the controversial NYPD tactic and his wife’s race may have helped him attract non-white voters to pass the 40 percent benchmark.

De Blasio is a tireless campaigner with years of experience in the game, and his views and stances on a wide range of issues make him popular with New Yorkers who seem to want a fairly liberal mayor to fight for the needs of lower- and middle-class residents after 12 years of Bloomberg repackaging the city as a “luxury product.”

No matter what she did, Quinn couldn’t shake the stigma of her close ties to Bloomberg, and by being aloof and non-committal both on the issues as well as in her decisions about whether or not to attend various debates and events, she managed to make herself irrelevant in a race in which she had been presumed the winner for fully two or three years.

And de Blasio nicely filled the gap she left behind, sticking to his guns and making his views felt as often and as forcefully as he could, and apparently it paid off for him in the end.

It’s difficult to nail down just what de Blasio offers that made him so attractive to voters this year, but the primary’s stunning outcome reinforces the fact that democracy is alive and well in Gotham, and no one is just given the keys to Gracie Mansion out of a sense that they've put in the time and now it’s their turn to run the city. Not even Carlos Danger would be so presumptuous in the wake of Tuesday’s result.