Ten years ago, a little-known state lawmaker in Upper Manhattan staged a major upset, winning a congressional seat that had for years been central to Black political power and making history as the first Dominican American and undocumented immigrant elected to the House.

Now, that history-maker, Representative Adriano Espaillat, is in a tough re-election fight of his own, resulting from the same type of shifting demographics that once propelled him to victory.

Mr. Espaillat, who has built the trappings of a small political machine and chairs the Congressional Hispanic Caucus, is facing a Democratic primary challenge from Darializa Avila Chevalier, an organizer and democratic socialist nearly 40 years his junior.

And as Mr. Espaillat, 71, works to blunt the ascent of Ms. Avila Chevalier, 32, another prominent democratic socialist has thrown his support behind her.

Mayor Zohran Mamdani endorsed Ms. Avila Chevalier in a joint interview with MS NOW’s Jen Psaki on Thursday evening.

“She grew up with a commitment to the very people that politics have left behind, and what I see in her is that commitment fulfilled,” he said, lauding her fights against immigration detentions and her support of his affordability agenda. “I can’t wait for her to be introduced to so many across the city and across this country as we fight for that affordability agenda, from New York City to D.C.”

Mr. Mamdani had committed to supporting Mr. Espaillat last year, according to three people familiar with the pledge. It came after the congressman dropped his support for former Gov. Andrew Cuomo’s mayoral bid and got behind Mr. Mamdani after he won the Democratic primary.

But after watching Ms. Avila Chevalier gaining momentum, the mayor reconsidered his decision.

The endorsement from Mr. Mamdani could further shake up the race by showering attention on a young upstart and activating a base of voters to head to the polls in what is likely to be a low-turnout election.

In a statement, Mr. Espaillat underlined his support from top New York Democrats, including Gov. Kathy Hochul, Attorney General Letitia James and the House minority leader, Hakeem Jeffries.

“Mayor Mamdani is entitled to support the candidate of his choice. But one endorsement does not make a race. Voters do,” he said. “I will continue doing what I have always done: listening to voters, showing up in every corner of the district and earning support by meeting voters where they are and fighting for the issues that matter most to them.”

The mayor has also endorsed Brad Lander in his challenge to Representative Dan Goldman in the 10th Congressional District and Claire Valdez in the Seventh District. He said during the interview on Thursday that Ms. Chevalier is his final congressional endorsement in New York. Primary Day is June 23.

The New York City chapter of the Democratic Socialists of America, which is supporting Ms. Avila Chevalier, is increasingly bullish about her prospects.

“There’s a hunger for our kind of politics in this community,” said Gustavo Gordillo, the chapter’s co-chair. “We saw it in the mayoral campaign.”

Mr. Gordillo recalled the excitement Mr. Mamdani generated in Washington Heights and elsewhere in Upper Manhattan when, nearing the end of his successful primary campaign last year, he walked the length of the borough in a highly publicized event.

Mr. Mamdani trounced Mr. Cuomo throughout the 13th Congressional District Mr. Espaillat represents, which runs through Upper Manhattan and the Bronx and has, in some areas, been gentrified considerably over the last decade.

Mr. Gordillo referred to Mr. Espaillat’s political operation, defined by loyalty he has cultivated from a group of city and state politicians, as “a bit of a paper tiger.”

Mr. Espaillat is, by several accounts from people who are supporting him, concerned about his re-election chances.

In a possible reflection of the tight race, Mr. Espaillat visited a federal detention center in New Jersey on Wednesday to side with activists who were part of a tense standoff with federal agents as they protested the harsh conditions inside the center. A video posted to social media shows the congressman entering the center with a court order.

In an interview Wednesday, ahead of Mr. Mamdani’s endorsement, Mr. Espaillat played down his opponent’s rise.

“I think I’m gaining some momentum and I think that I’m surging,” he said, referring to his lead in an internal poll conducted by Ms. Avila Chevalier’s campaign in April. “I’m growing every day. I’m out in the street, I’m knocking on doors, I’m doing the subways, I have a strong ground operation and I think I’m going to win big.”

“So I don’t see her surging at all,” he said, adding, “The love and support I’m getting in the street, I’ve never seen before.”

Mr. Espaillat sidestepped questions about Mr. Mamdani’s potential involvement in the race, saying he and the mayor generally centered their discussions on policy matters, including child care, transportation and immigration.

Shaun Abreu, a city councilman who considers Mr. Espaillat a mentor, described him as “an important Latino figure in Washington with meaningful seniority chairing the Hispanic Caucus and sitting on the appropriations committee.”

“That’s a big advantage for our community to throw away,” Mr. Abreu added. “He has strong relationships with Hakeem Jeffries, Alexandria Ocasio-Cortez and Governor Hochul, which is why we’ve been able to get historic investments uptown and in the Bronx.”

The stakes for Mr. Mamdani are high. If Ms. Avila Chevalier pulls off an upset, he will have helped democratic socialists expand their presence in the House. But if Mr. Espaillat wins, he will have alienated a senior member of New York City’s congressional delegation at a time when the city desperately needs federal aid.

Those familiar with his thinking said Mr. Mamdani appreciated Mr. Espaillat’s swift decision to endorse him after he defeated Mr. Cuomo in the primary, even though Mr. Cuomo stayed in the race as an independent candidate. Mr. Espaillat’s support was a boon to the mayor in his efforts to court Latino voters, a key voting bloc.

A lack of public polling on the race makes it hard to determine who is in the lead, but several metrics show Ms. Avila Chevalier has momentum.

Her campaign raised more money than Mr. Espaillat’s in the first quarter of the year, though he has more cash left to spend as of the latest campaign filing.

Mr. Espaillat recently expressed concerns to several colleagues about a tightening race after seeing the results of his internal polling, according to three people familiar with those conversations. In the interview, Mr. Espaillat would not answer questions about his own polling.

After a campaign rally on Monday, Ms. Avila Chevalier said her campaign had “wind in our sails,” adding that she felt there was “the community power to really take this over the finish line.”

Asked about a possible endorsement from the mayor, Ms. Avila Chevalier demurred, saying she was “really focused on building the support on the ground.”

She added: “At the end of the day, it’s the voters of New York 13 that are going to make the decision around who’s going to represent them in Congress.”

New York’s democratic socialists have sought to expand their footprint citywide and has seen its fastest growth in the 13th District, feeding leaders’ enthusiasm for a challenger like Ms. Avila Chevalier, who has been a member since last year. Her campaign plans to hold more than a dozen canvasses each week leading up to Primary Day.

The race for the seat is shaping up to be more expensive in its final weeks. On Wednesday, Justice Democrats, the group that helped power the rise of Ms. Ocasio-Cortez, announced that it planned to spend $250,000 on ads for Ms. Avila Chevalier, bringing its total spending on the race to more than $380,000. And on Thursday, BOLD PAC, an organization affiliated with the Congressional Hispanic Caucus, announced that it would spend $600,000 to support Mr. Espaillat.

In a statement, Usamah Andrabi, a spokesman for the group, said that voters would support Ms. Avila Chevalier against “machine politicians funded by AIPAC & the real estate lobby like Adriano Espaillat and Andrew Cuomo, and are ready to usher in a new era led by courageous, unbought leaders like Darializa.”

Ms. Avila Chevalier, who helped organize protests at Columbia University against the war in Gaza, has condemned the support Mr. Espaillat receives from AIPAC, the influential pro-Israel lobbying group, and argued that he is too beholden to real estate interests and corporations.

Mr. Espaillat, asked about AIPAC’s donations to him, said, “no one that donates to my campaign has control over how I vote. Only my constituents tell me how to vote.”

Ms. Avila Chevalier’s stances on the matter have also drawn criticism. During a candidate forum in January, she would not condemn Hamas’s fatal attack on Israel on Oct. 7, 2023. She also criticized Ms. Ocasio-Cortez in now-deleted posts over her past support for funding Israel’s Iron Dome defense system.

As she gains momentum, Ms. Avila Chevalier has faced scrutiny over other past comments on social media.

The New York Post recently drew attention to years-old posts in which she criticized interracial marriage and talking about slamming the door on an “old white lady.” In a now-deleted post, Ms. Avila Chevalier was also critical of Vice President Kamala Harris, condemning her comments about migrants to the United States using an expletive.

“I think the fact that The New York Post has ignored the issues that we’re talking about today in favor of tweets that I wrote when I was very young just goes to show that they are, like so many of our elected representatives and establishment politicians, just continuing to ignore what people here actually need and deserve,” Ms. Avila Chevalier said in response.

In a bit of late campaign season intrigue, a pro-Espaillat flier emerged on Tuesday depicting Ms. Avila Chevalier with an image of someone else. Mr. Espaillat’s team claimed ignorance about who was behind it, saying the campaign had not funded the mailers. Ms. Avila Chevalier responded on social media with a shrug emoji.

Jon Paul Lupo, a Democratic consultant unaffiliated with the race, said he was surprised by how competitive it appeared to have become.

“People are looking for fighters,” Mr. Lupo said. “They’re mad at the status quo and it seems to have created at a minimum a very tight race.”

Nicholas Fandos contributed reporting.