Mayor Vincent C. Gray never escaped the shadow of the illegal scheme that helped him win his office in 2010. He never convinced the half of the city that voted against him four years ago that there was meat to his “One City” slogan. And in the final, decisive three weeks of the primary campaign that ended with his resounding defeat Tuesday, Gray failed, even on his home turf, to answer many voters’ doubts about his honesty.

Whether Gray directed the scheme that has resulted in five of his campaign associates pleading guilty in federal court to violating election laws, or, as he contends, was ignorant of what was being done in his name, Democratic voters punished the mayor for the scandal, choosing instead a relatively unknown D.C. Council member, Muriel Bowser (Ward 4).

“When all the dust settles, he’s going to be indicted,” said Rusty Mason, a retired postal worker who voted for Bowser in the mayor’s home precinct of Ward 7, where Gray won more than 80 percent of the vote four years ago. “He’s just had too much controversy, seems a little shady. Even if he didn’t know what was going on, he should have.”

Only a decade ago, Gray, then 61, was a newcomer to political campaigns, running for the council seat in the ward where he lived and worked as director of Covenant House, a nonprofit group serving homeless youths. Gray had spent most of his career as a D.C. agency head and an advocate for some of the city’s neediest citizens.

His reputation was sterling, his motives rarely questioned. At a time of life when many of his peers were winding down, Gray beat an incumbent council member, won the respect of his colleagues, rose to the council’s top position and then ousted a sitting mayor who had four years earlier won the most lopsided victory in D.C. history.

D.C. Council member Muriel Bowser captured the Democratic nomination for D.C. mayor on Tuesday. PostTV talks to her supporters and Mayor Gray's defenders about what the city would look like under a Bowser administration. (Theresa Poulson and Gabe Silverman/The Washington Post)

But how Gray beat Adrian M. Fenty in 2010 quickly became the subject of a federal investigation. The mayor’s bid for reelection took a nose dive on March 10 — known inside the Gray campaign as Stormy Monday — when prosecutors said that Gray personally asked a prominent city contractor for illegal donations.

A mayor known for his elegance, a man given to monogrammed shirt cuffs and hand dancing, became the butt of jokes among those who wanted him gone and the subject of downcast disappointment among his most loyal fans.

“I’ve been listening to his explanations, and my gut feeling is he did what they said he did,” said Johnnie Cross, who lives near Gray’s house in Hillcrest in Southeast and voted for Bowser. What sealed Cross’s vote was Gray’s admission that he referred to businessman Jeffrey E. Thompson, the money man behind the shadow campaign, as “Uncle Earl.”

“He admitted he called the man ‘Uncle Earl,’ ” Cross marveled. “The mayor of my city, calling a grown man uncle.” On Tuesday, Gray got fewer than half as many votes in his home precinct as he did in 2010.

On Tuesday, Bowser’s campaign looked like Fenty’s redux, an energetic outpouring of sign-shaking, slogan-chanting workers standing in median strips and ushering voters to the polls. Some Bowser partisans even wore their old Team Fenty T-shirts. Gray’s effort, by contrast, looked desultory, small groups of mostly older workers, present but listless.

Now, Gray stands rejected, a lame duck, awaiting what his own attorney called likely indictment and trial, perhaps while he is still in office. Gray has said in recent days that he will not leave office if he is charged; D.C. law would require him to do so only if he pleads guilty to or is convicted of a felony.

Long before his December decision to seek a second term, Gray’s legal woes had attracted a field of seven challengers, including four of the 13 council members. For a while, it looked as if Gray might slip past the pack by relying on bedrock support east of the Anacostia River.

But U.S. Attorney Ronald C. Machen Jr.’s decision to spell out his case against Gray three weeks before the primary recast the race instantly.

Was the mayor a crook? Six in 10 likely primary voters told Washington Post pollsters that they believe the allegations leveled against Gray by Thompson. And council member Jack Evans (Ward 2), who was ostensibly running against the mayor but praised his stewardship of the city at nearly every appearance, warned audiences that the District is staring into “a very rough summer” if the mayor is put on trial.

Voters across the city struggled also with the tension between a booming economy and an embattled mayor who refused to explain what happened in his 2010 campaign beyond, “I didn’t do anything wrong.” Even as 62 percent of D.C. residents concluded that the city was moving in the right direction, 61 percent said Gray was not honest and trustworthy.

In Gray’s home base, it wasn’t just the scandal that damaged him. “He claims he’s doing so much for us, but look at the unemployment,” said Mason, the retired postal worker. “The city’s getting rich, but over here, it’s all the same.”

To voters with doubts, any cracks that appeared in the city’s veneer of economic success made the mayor seem less suited for a second term. The tragic disappearance of an 8-year-old girl whose family lived in the District’s overcrowded shelter at the old D.C. General Hospital focused attention on the Gray administration’s difficulty in coping with a surge in the city’s homeless population this winter. A series of frightening failures by the city’s fire-EMS department — and Gray’s insistent support of his beleaguered fire chief — led some residents to wonder why a city boasting a large surplus couldn’t provide steady basic services.

Unable to shake accusations of corruption, Gray reacted in the last two weeks by focusing on his base in Wards 7 and 8, where he won four-fifths of the vote in 2010. The strategy, crafted in consultation with council member Marion Barry (Ward 8), who made a career of finding electoral gold in the city’s racial and class divisions, made theoretical sense: Washington remains a city where longtime black residents tend to vote more reliably in local elections than white newcomers.

But the approach also ran the risk of backfiring as voters of all races and classes saw Gray abandon his “One City” slogan and market himself instead as “One of Our Own,” the name of a campaign video in which the mayor appeared only with black residents.

Gray “believes in ‘one city,’ ” Barry said Tuesday night, “but we don’t have one city.”

From Saturday evening through Election Day, workers delivered shiny new Supercan trash receptacles to affluent upper Northwest neighborhoods whose residents didn’t expect the cans until summertime — a move that some homeowners viewed as political desperation rather than evidence that Gray ran the government well.

“To some in our city, I’m just another corrupt politician from the other side of town,” Gray said in his State of the District address two days after prosecutors detailed allegations against him.

His base heard that message and rallied around the mayor they knew as a good man. “Anybody can make stories up,” said Susan Woodard, voting in Gray’s home precinct. “Whatever happened, that’s between him and the Lord Jesus Christ Almighty God.”

But Gray’s campaign, although well-funded, lacked the organizational oomph it had four years ago. Buses that plied the streets in recent days seeking early voters often remained nearly empty.

Chuck Thies, Gray’s campaign manager, said the return of scandal to the city led many voters to stay home.“There has been an ‘eww’ factor and that turns people off,” he said.

As doubts about Gray mounted, Bowser, Fenty’s 41-year-old protege, used the mayor’s troubles to present herself as the clean, young, credible alternative. She polished her once-bureaucratic rhetoric; fielded a highly visible, Fentyesque street campaign; and presented herself as a natural heir to Mayor Anthony A. Williams and Fenty and their effort to build a gleaming new city of condos, Class A offices and amenities that attract young, affluent professionals.

This election was something of a replay of the faceoff between Fenty and Gray: a pure political power tussle, fueled by generational and class rivalries.

“If you took Fenty’s name out and put Bowser’s name in, you’d have the exact same campaign, the same operation,” said George Johnson, executive director of the D.C. unit of AFSCME, the government workers union, which endorsed Gray.

But Johnson said the city’s unions and other Democratic leaders will now rally around Bowser, “without a doubt, without hesi­ta­tion. That’s politics.”