Ms. Walton, 38, a democratic socialist who has never held political office, defeated Byron Brown, a four-term incumbent.India B. Walton knew her bid to unseat the entrenched 16-year mayor of Buffalo was a long shot.A registered nurse and community activist, Ms. Walton’s life was defined by hardship: a teenage single mother at the age of 14, a high school dropout, resident of a group home and a victim of domestic violence.A self-described democratic socialist, Ms. Walton, 38, has never held political office, and she was challenging Mayor Byron Brown, 62, who was seeking a fifth term, had served as chair of the state Democratic Party and was once was mentioned as a candidate for lieutenant governor. Few people thought she could win. Mr. Brown mostly tried to ignore her campaign.But on Tuesday, Ms. Walton defeated Mr. Brown in the city’s Democratic primary, making it almost certain that she will become not only the first woman elected mayor in New York State’s second-largest city, but also the first socialist at the helm of a large American city in decades.Her upset on Wednesday shocked Buffalo and the nation’s Democratic establishment as most of the political world was more intensely focused on the initial results of the still-undecided mayoral primary in New York City. Her win underscored the energy of the party’s left wing as yet another longtime incumbent in the state fell to a progressive challenger, echoing the congressional wins of Alexandria Ocasio-Cortez and Jamaal Bowman.If Ms. Walton wins in the general election in November — a likely result in a city that leans heavily Democratic — she would join the growing ranks of Black female mayors elected to lead other major U.S. cities, including Lori Lightfoot in Chicago, Kim Janey in Boston and London Breed in San Francisco.“I don’t think reality has completely sunk in yet,” Ms. Walton said on Wednesday in a phone interview shortly after receiving a congratulatory call from Senator Chuck Schumer of New York.“I’m India from down the way, little poor Black girl who, statistically speaking, shouldn’t have amounted to much, yet here I am,” she added. “This is proof that Black women and women belong everywhere in positions of power and positions of leadership, and I’m just super-excited.”Ms. Walton, whose campaign was backed by the Working Families Party and the Democratic Socialists of America, said she preferred not to get caught up in the semantics of labels — describing her ideology as focused on “putting people first.”The last time a socialist was the mayor of a large American city was 1960, when Frank P. Zeidler stepped down as Milwaukee’s mayor. And it was more than a century ago when a socialist won a mayoral race in New York: In 1911, George R. Lunn, of the Socialist Party of America, was elected mayor of Schenectady, according to Bruce Gyory, a Democratic political consultant.While rare, socialist mayors are not unheard-of: Bernie Sanders took office in 1981 as mayor of Burlington, Vt., a city one-sixth the size of Buffalo, before being elected to Congress nearly a decade later.Ms. Walton ran an unabashedly progressive campaign in a Democratic city of about 250,000 people — about 37 percent of them Black — that had elected mostly white men as mayors for nearly two centuries. (Mr. Brown became the city’s first Black mayor in 2006.)She said she supported implementing rent control protections. She pledged to declare Buffalo a sanctuary city for undocumented immigrants. And she vowed to reform the city’s Police Department, arguing in favor of an independent civilian oversight board and changing the way police officers respond to mental health calls.“Our police budget is as high as it’s ever been, and crime is also up, so something is not working,” she said.There were a number of factors that both Ms. Walton’s supporters and critics agree helped catapult her to victory: Turnout among Democratic voters in Buffalo was very low, about 20 percent, and Ms. Walton raised money and organized effectively to build a multiracial coalition, including Black voters that would have typically voted for Mr. Brown.Mr. Brown’s actions suggested that he did not take Ms. Walton’s challenge seriously. He refused to debate her — “Maybe he believed pretending I didn’t exist was going to make the race go away,” Ms. Walton said — and he did not campaign vigorously, failing to fund-raise as aggressively as he had in previous primaries or spend on ad buys until late in the race.“I think it was almost a perfect storm that was working against the mayor in this case, but it was brought about by his nonchalance in this race,” said Len Lenihan, the former Erie County Democratic chairman.On Wednesday, Gov. Andrew M. Cuomo, who controls the state’s Democratic Party and is a longtime ally of Mr. Brown — he picked him to chair the state party in 2016 — seemed to agree with that analysis.“His campaign strategy, as I understand it, was to avoid engaging in a campaign,” Mr. Cuomo said during a Manhattan news conference, adding, “We’ve seen that movie before.”The Associated Press called the race on Wednesday after all the in-person votes had been counted and Ms. Walton led by seven percentage points. Mr. Brown refused to immediately concede on Tuesday night, saying absentee ballots still needed to be counted; his campaign did not make him available for an interview on Wednesday.But Jeremy Zellner, the chairman of the Erie County Democratic Party, said he had spoken to Mr. Brown on Wednesday and that the mayor may be considering a write-in campaign in the November general election. Mr. Zellner, however, said he informed Mr. Brown that he had pledged his support to Ms. Walton.Under Mr. Brown, Buffalo, in western New York, has undergone a resurgence in recent years with the construction of major projects in the downtown area. But the city’s poverty rate is more than twice the national average, and its unemployment rate, while improving, has not fully recovered to prepandemic levels.Indeed, there was a sense among some residents who voted for Ms. Walton that low-income communities were not reaping the benefits of downtown development.“Buffalo is super-stagnant,” said Anthony Henry, 29, a musician and student. “We try to talk like there’s a lot of progress going on, with recent developments along the waterfront, but nothing has moved.”That stagnation included Mr. Brown, some voters said. “I’m a firm believer that people shouldn’t be in power too long, we need to have fluidity in government,” said George Olmsted, 59, a middle-school teacher. “A lot of people throw this word ‘socialism’ out there like a weapon, but hello, we have Social Security, we have public-funded education in America.”Upstate New York has large swaths of rural and conservative areas, but many of its cities are reliable Democratic strongholds with large minority communities that left-wing activists see as fertile ground to replicate the upsets they have staged downstate. So far, democratic socialists have picked up seats in the House, the State Legislature and the New York City Council, but Ms. Walton’s win would mark the first time a D.S.A.-backed candidate won a citywide election in New York.Ms. Walton’s win was also buttressed by extensive support from the Working Families Party, which had previously endorsed Mr. Brown. The party helped her campaign set up an online fund-raising operation, a large field program with hundreds of volunteers and a text message and phone bank operation that made 19,000 calls on the night before the election — in a contest where fewer than 25,000 voters cast ballots.She proved to be a formidable fund-raiser, garnering more than $150,000 in campaign contributions, a respectable haul for a first-time candidate who had little name recognition at the beginning of the race.Charlie Blaettler, the elections director at the statewide Working Families Party, said that Ms. Walton’s deep relationships in the community made her the right candidate to run against an entrenched incumbent.“This race is a testament to India as a person and the moral clarity with which she speaks,” Mr. Blaettler said. “It shows how important it is for the left to run people who are not just saying the right things, but who have been there for years, doing the work, organizing on the ground.”Ms. Walton made a name for herself as the executive director of a community land trust in a neighborhood of the low-income East Side near downtown Buffalo that has seen an influx in development, leading to a sense among African-Americans that their community was threatened by gentrification.As the middle child to a single mother, Ms. Walton looked after her younger siblings growing up. At 14, she became pregnant and went to live at a group home for young mothers for two years before moving with her young son to her own apartment.She later got married and, at 19, gave birth to twin boys who were born prematurely and had to spend six months in the hospital. That experience inspired Ms. Walton to become a nurse before becoming a community leader and organizer.“I’ve gone through a lot of challenges, from being a teen single mother to overcoming domestic violence. I believe that every challenge that I have faced in life has prepared me to be able to reach back and help someone else,” Ms. Walton said. “This campaign is really centered on the principle of lifting as we climb.”Ms. Walton is an organizer for activist groups that supported the state’s bail reforms and legalizing recreational marijuana. Last summer, she gained exposure marching against police brutality in the protests following George Floyd’s death.She ultimately decided to run, she said, because Mr. Brown had failed to implement meaningful reforms at the Buffalo Police Department and because of what she saw as his poor response to the coronavirus pandemic.“It was, like, why not?” she said. “Someone has to do it.”Michael D. Regan contributed reporting. More