Bowser tapped into an electorate that tired of the allegations surrounding Gray. Five people who worked on the mayor's 2010 campaign have pleaded guilty to felonies, and Bowser said the city needed a fresh start as the mayor faced potential criminal charges.
Bowser, 41, is a D.C. councilmember and a protege of former Mayor Adrian Fenty, whom Gray defeated in 2010.
The Democratic primary winner has gone on to win every general election since the district began electing a mayor 40 years ago. But Bowser will face a credible challenger this November in David Catania, an independent D.C. councilmember.
Gray defeated Adrian Fenty in 2010 by tapping into dissatisfaction among African-American residents. But a series of guilty pleas in federal court have revealed that top campaign aides broke the law to help him get elected. Three weeks ago, prosecutors said Gray knew about an illegal, $668,000 slush fund that aided his get-out-the-vote efforts four years ago.
Gray has not been charged and insists he did nothing wrong. His attorney has said he is preparing for a possible indictment.
"It's too much. We've gone through scandals before in D.C., and we don't need any more," said Rufus Okunubi, 68, a cab driver who backed Gray in 2010, but voted for Bowser this time.
Bowser pledged to unite the party following the divisive primary campaign.
"The residents of the nation's capital have always elected a Democratic mayor, a Democratic president, and in big numbers, and we're going to do it again in November," she said to supporters gathered at a charter school in southeast Washington.
In his concession speech, Gray told a subdued crowd at a downtown hotel that he would continue working hard during the last nine months of his term.
"The amount of work that we've done over the last three-and-a-quarter years has been nothing short of phenomenal," the mayor said.
Many Gray supporters view U.S. Attorney Ronald Machen's office with suspicion and said it was unfair for prosecutors to accuse the mayor of wrongdoing without charging him with a crime.
"I'm disappointed, of course, about the election in 2010, but I'm also disappointed with that U.S. attorney for taking so long to do what he's going to do," said Sandra Humphrey, who voted for Gray.
Some Bowser voters, meanwhile, said they backed her primarily because they felt she had the best chance to beat Gray. Joan Gladden, 65, said she voted for Gray in 2010 and would have stuck with him if not for the allegations of corruption.
"Do we have any honest politicians left?" she said.
Bowser worked for the local government in suburban Montgomery County, Md., and served as an elected neighborhood commissioner in the district before election to the council in 2007.
Opponents said Bowser lacks experience to be mayor, saying her legislative record is skimpy. Her most significant accomplishment on the council was the creation of an independent ethics board able to punish officials for violations. The board has found wrongdoing by three members of the 13-person council.
Catania, 46, has pledged a substance-based campaign and pointed to his leadership on issues such as gay marriage, medical marijuana, reducing the number of residents without health coverage and creating a college scholarship program for city high school students.
Gray, 71, led nonprofit organizations and the city's Department of Human Services before he was elected to the D.C. Council in 2004. As mayor, he's known as a pragmatic, detail-oriented technocrat and sound manager of the city's robust finances. The district has enjoyed a surging population, a booming real estate market and relatively low violent crime.
Turnout for the primary was light, and some voters said they were unimpressed with the slate of candidates. Many observers blamed the unusually early primary date for dampening enthusiasm and making campaigning difficult. The candidates also included councilmembers Jack Evans and Tommy Wells, either of whom would have become the city's first white mayor if elected.
"I voted for Bowser. I held my nose," said Eugene Gill, 52, a retired city worker. "All of them are terrible."
Catania, a former Republican who has championed progressive causes since leaving the party in 2004, spent the day shaking voters' hands at several precincts.
"No one wants to vote today," he said. "It's a little bit disheartening to see the light turnout. It's a function of people losing faith in the system."
Associated Press writers Jessica Gresko and Brett Zongker contributed to this report.
Follow Ben Nuckols on Twitter at https://twitter.com/APBenNuckols.