© Victor J. Blue for The New York Times Mayor Bill de Blasio of New York exited the race for the Democratic presidential nomination on Friday.

Mayor Bill de Blasio, who entered the Democratic presidential race on the premise that his brand of liberal urban leadership in New York City could appeal on a national scale, said on Friday that he was ending his candidacy.

Mr. de Blasio had hoped to seize the progressive energy rippling through the Democratic Party, using his accomplishments in New York — including the introduction of universal prekindergarten and paid sick leave and raising the minimum wage — to make his case.

Sign Up For the Morning Briefing Newsletter

But he remained in the bottom tier among nearly two dozen candidates, unable to match the popularity of Senators Bernie Sanders and Elizabeth Warren, the prime architects of his party’s leftward shift.

Mr. de Blasio said on Friday that he was satisfied that his message seemed to resonate with people he met on the campaign trail. But his continued candidacy, in the face of dismal poll and fund-raising numbers, drew criticism from many circles, especially in New York.

“I feel like I’ve contributed all I can to this primary campaign, and it’s clearly not my time,” he said during an appearance on MSNBC’s “Morning Joe” program. “I’m going to end my presidential campaign, continue my work as mayor of New York City, and I’m going to keep speaking up for working people.”

Mr. de Blasio had focused his run on trying to improve the lives of those people, proposing a “workers’ Bill of Rights” to guarantee Americans paid time off and medical leave, and vowing to “tax the hell” out of wealthy people to pay for his plan.

He tried to position himself as the most suitable Democrat to take on President Trump, given his familiarity with Mr. Trump as a New York real estate magnate. Mr. de Blasio branded the president “Con Don,” and highlighted how he had already fought the Trump administration on issues like climate change and immigration.

None of it worked.

Mr. de Blasio’s campaign, seen as a quixotic, 100-to-1 shot from its inception, never gained traction, not even in New York. Fliers appeared at the gym in Park Slope, Brooklyn where he works out, urging him not to run (and to wipe down the equipment after he finished using it).

A recent poll of registered New York State Democrats by Siena College found that fewer than 1 percent favored the mayor as the Democratic nominee.

Mr. Trump greeted the news with sarcasm, characterizing Mr. de Blasio’s withdrawal as “really big political news, perhaps the biggest story in years!”

“NYC is devastated," the president wrote on Twitter. “He’s coming home!”

At a news conference in the Oval Office, Mr. Trump continued to needle Mr. de Blasio, calling him a “part-time mayor” who could now “work a little bit harder.”

“He dropped out of the presidential race a little while ago,” Mr. Trump told reporters. “Too bad, he had tremendous potential. He only had one real asset. You know what it was? Height. Other than that, he had nothing going.”

Mr. de Blasio’s initial campaign finance filing showed that he had raised only $1.1 million, with much of the money coming from the sole city union supporting him. The union — like some of Mr. de Blasio’s other donors — had, or could in the future have, business before the city.

In one fund-raising period, another Democratic presidential hopeful, Mayor Pete Buttigieg of South Bend, Ind.raised twice as much money from New York City residents as Mr. de Blasio raised nationally.

Even as better-financed rivals like Senator Kirsten Gillibrand of New York dropped out of the race after concluding that it was “important to know when it’s not your time,” Mr. de Blasio held on and argued that one viral moment on social media could give his campaign a lift.

“People go from unheard-of to totally famous in 72 hours in America now,” Mr. de Blasio said at a news conference in September, when he first acknowledged that the end of his candidacy might be in sight.

But it soon became apparent to the mayor that he would not qualify for the fourth Democratic debate next month after failing to qualify for the previous debate, in September.

“As we went over the last few weeks, every day that passed, it got tougher,” Mr. de Blasio said at a news conference outside Gracie Mansion on Friday, standing next to his wife, Chirlane McCray. “There wasn’t more progress. We were watching the polling to see if anything was moving. It just wasn’t moving.”

Combative interviews with the Fox News hosts Sean Hannity and Tucker Carlson did not increase his standings in the polls or his ability to raise money. Protesters, upset that Mr. de Blasio had refused to fire the police officer who placed Eric Garner in a chokehold before he died, interrupted both a national debate and a CNN town hall where the mayor was a participant.

“This is what democracy looks like and no one said it was pretty,” the mayor wrote on Twitter in response to the protest in July at the second Democratic debate.

Even Mr. de Blasio’s successes on the campaign trail were marred by mishaps. After being praised for his performance at the first debate, in Miami, Mr. de Blasio quoted the Cuban revolutionary Che Guevara, during a union protest the next day, angering members of the local anti-Castro Cuban community.

New Yorkers repeatedly questioned whether Mr. de Blasio should travel from rural Iowa to the Nevada desert to address small groups about the travails of working people when the largest city in the United States was facing serious problems like rising homelessness and a public housing crisis.

The mayor’s whereabouts became an issue in July, when a 13,000-volt cable fire caused 72,000 New Yorkers to lose power in Manhattan. At the time, Mr. de Blasio was delivering a stump speech at a union hall in Waterloo, Iowa, and fretted about returning to the city.

He offered a live update on CNN, and allowed Corey Johnson, the City Council speaker and a would-be successor to Mr. de Blasio, to provide New Yorkers with constant and precise updates via social media. The mayor was unable to return to New York until the next day, hours after power had already been restored.

“New Yorkers want their mayor to be a national figure, but they want him to do it from the steps of City Hall because they want the garbage picked up,” said Sid Davidoff, a supporter of Mr. de Blasio’s who was deputy campaign manager for John V. Lindsay, the last person to run for president while being New York City’s mayor.

Scott M. Stringer, the New York City comptroller who plans to run for mayor in 2021, said that Mr. de Blasio’s presidential candidacy had damaged his mayoralty.

“I think it’s a genuine setback,” Mr. Stringer said in an interview. “He was chasing this notion of being a national progressive political icon, but that ship had sailed in the name of Elizabeth Warren and Bernie Sanders. He has the greatest job in the greatest city in the world. He needs to come back and just govern the hell out of New York City.”

Mr. de Blasio expressed no regrets over his failed campaign, asserting that he was one of a few progressive candidates who had helped move the Democratic agenda to the left.

Jon Paul Lupo, Mr. de Blasio’s campaign manager, said that the mayor would continue to try to influence national politics. Mr. Lupo said he would now lead the mayor’s federal fund-raising entity, Fairness PAC, as Mr. de Blasio sought to raise money for progressive Democrats. It was unclear whether the mayor would still travel the country.

On Thursday, the Federal Election Commission sent Mr. de Blasio’s campaign a letter asking for an explanation of its use of money from his state political action committee, NY Fairness PAC. A New York State ethics panel is also investigating the mayor’s fund-raising practices.

As for his campaign, Mr. de Blasio said that if he had it to do again, there was only one thing he would do differently: Enter the race sooner.

“I wish I had more time,” he said. “I wish I had more resources. I think that would have made a difference. Earlier is better.”