Ms. Pressley, who in 2009 became the first black woman elected to the Boston City Council, overcame a powerful lineup of the Massachusetts political establishment. Mr. Capuano, 66, who has held the seat for 20 years, was endorsed by almost every major political figure, including Mayor Martin J. Walsh of Boston, who deployed his extensive political machine on Tuesday on Mr. Capuano’s behalf.

“This is a big wake-up call to any incumbent on the ballot in November,” said Mary Anne Marsh, a Boston-based Democratic strategist. “We’ve been in a change election cycle for years. But Trump may have opened the door for all these young candidates, women, people of color, because voters want the antithesis of him.”

Ms. Pressley’s win, the margin of victory, and the historic nature of her candidacy are sure to reverberate throughout Boston, a city whose fraught racial history is baked into its national reputation. Ms. Pressley said Democrats throughout the state discouraged her from running against Mr. Capuano, and John Lewis, the civil rights legend and longtime Georgia congressman, held a campaign event for him in May. Yet Ms. Pressley rode a strong turnout among Boston’s minority communities toward history.

Her slogan, “change can’t wait,” was a nod to those who said her candidacy was disrupting the traditional order of Boston politics, she said. It was also a rallying cry for the state’s only minority-majority district — that it should have a representative who mirrors the community’s diversity.

Political observers said the win was the biggest sign yet that a “new Boston” was emerging in the shadow of the city’s historically white, union-driven political establishment. This new electorate is powered by minorities, immigrants and young college students who have flocked to the city’s start-ups and tech-friendly industries.

“It felt like a good time to give someone who’s not a white male a shot,” said Linus Falck-Ytter, 26, a software developer, after voting in Cambridge. “And I liked that she’s more outspoken about helping underrepresented communities.”