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.