Bill de Blasio — who swept into office in a landslide victory four years ago after promising an unabashedly liberal government — on Tuesday night became the first Democratic mayor in New York City to win re-election since Ed Koch in 1985.

With 98.4 percent of voting scanners counted shortly before 1 a.m. Wednesday, 66 percent of the vote went to de Blasio and 27.7 percent to Republican Nicole Malliotakis, according to the city Board of Elections. The BOE counted a total of 1,097,846 ballots, which is about 22 percent of registered voters in the city.

“We have so much to be proud of, but we can’t stop now,” de Blasio said to a cheering crowd late Tuesday night at the Brooklyn Museum. “But tonight there are too many of our fellow New Yorkers who feel the deck is stacked against them. . . . The truth is, they are right. We have to become a fairer city, and we have got to do it fast.”

De Blasio’s victory capped a sleepy race that pitted the first-term incumbent, 56, against Malliotakis, 36, a state assemblywoman from Staten Island, and Bo Dietl, 66, a former NYPD cop turned reality TV star and private detective, running on the “Dump the Mayor” line. Dietl won less than a percent of votes, with lesser-known candidates earning the balance of support, according to the BOE.

The polls closed at 9 p.m., and The Associated Press declared de Blasio the winner about 25 minutes later.

The mayor said his re-election was a rejoinder to skeptics of his 2013 run who predicted “things would go in the wrong direction” if he won: The economy would tank, crime would rise.

“Well, they were wrong,” he said, promising his second term — the final under the city’s term-limits law — would be dedicated to making New York “the fairest big city in America.”

Citing his own victory, as well as governorships going to Democrats in New Jersey and Virginia, de Blasio also said he considered the election results a rebuttal to President Donald Trump.

“You can’t take New York values on and win, Mr. President!” he said.

In a concession speech to about 100 supporters at a Williamsburg, Brooklyn hotel, Malliotakis promised to “continue to fight for the taxpayers of this city.”

“We may not have won this race, but we made our voices heard,” she said.

Every opinion survey published this year had shown de Blasio defeating his declared opponents by double-digit percentages. The latest, released Nov. 1 from NY1 and Baruch College, put de Blasio 33 percentage points ahead.

In a city where Democrats outnumber Republicans 6 to 1, de Blasio’s election was an affirmation of partisan politics as usual for New York City, after a 20-year exile from power as non-Democrats Rudy Giuliani and Michael Bloomberg assumed the role of mayor, said Ken Sherrill, professor emeritus of political science at Hunter College in Manhattan.

“It was an election in which all that seemed to matter about the candidates was that one of them was a Democrat and one of them was a Republican,” Sherrill said. “I don’t think that any issues dominated the election.”

De Blasio has provided few new policy initiatives for a second term but promised to build on what he’s done in his first four years.

He wants to expand the city’s free pre-kindergarten classes currently available to all city 4 year olds with eventual expansion to include 3 year olds. He’d keep municipal rules, started in the first part of his term, to block federal immigration agents from detaining nearly all arrestees who are living in the United States illegally. His pledge to close Rikers Island, the city’s jail complex, by 2027 — long after he’s out of office — has been met with skepticism from criminal reform advocates.

Malliotakis, Dietl and other third-party opponents, cast de Blasio as a lethargic and inept leader — too ideological, frequently tardy and derelict in his day-to-day municipal duties in favor of out-of-town trips to promote causes like combating inequality and protesting Trump.

Trump, who was in Seoul, South Korea, on Tuesday, voted via absentee ballot in his hometown’s election, White House press secretary Sarah Huckabee Sanders told Newsday. She would not say who he backed for mayor.

De Blasio voted Tuesday with his wife in Brooklyn. As dozens of activists from the group #CLOSErikers — chanted “Mayor Bill de Blasio, we’re calling out your name! Ten years of pain! Ten years of shame!” he broke with his long-running practice of entering the Park Slope library polling site by the front door, instead entering the building from the rear. 

Asked after voting why he went in the back door, de Blasio said, “We wanted to keep it simple today.”

In March, de Blasio reversed his stance on closing down Rikers, which the U.S. Justice Department has said is beset by a “deep-seated culture of violence” and corruption.

De Blasio has frustrated activists by insisting that the closure would take 10 years — long after he’s out of office — and punting to a future mayor the politically fraught choice of where to open replacement lockups and whether to actually shutter the complex.

The state’s former chief judge Jonathan Lippman, who oversaw a panel that recommended closing Rikers, wrote in an opinion piece in amNewYork in October the timeline could be shorter and that de Blasio’s plan “won’t get us across the finish line.”

De Blasio said the prospect of being the first Democrat since Koch 32 years ago to win another term “means a lot.”

“We have to be more deeply connected to the grass roots,” he said. “We need a clear progressive message, and a message about changing people’s economic reality.”

Malliotakis arrived at her Staten Island polling station earlier Tuesday to a warm reception, receiving hugs from supporters on hand at the Shirlee Solomon School where she cast her vote just before 7 a.m. She was joined by her parents, George and Vera, immigrants of Greece and Cuba respectively, who both wiped tears from their eyes after their daughter cast her vote.

“They came as immigrants to this country, not speaking the language, not having family . . . and yet today they’re casting their vote for their daughter, one generation later, to be mayor of the City of New York,” Malliotakis told reporters. “That shows you how truly special New York is and we must keep New York City as a place of opportunity, and we must restore it as a beacon of hope.”

With Emily Ngo