Sen. Edward J. Markey, recast as the insurgent liberal after more than four decades in Congress, made history Tuesday as the first politician to beat a Kennedy in a statewide election in Massachusetts.

Markey secured the Democratic nomination for the Senate, turning back a challenge from Rep. Joe Kennedy, a scion of the political dynasty long seen as a rising star. With 43 percent of the precincts reporting, Markey led 55 to 45 percent when the Associated Press projected the incumbent as the winner.

The outcome was a coup for the liberal wing of the party, which rallied its resources behind Markey, even as some establishment Democrats, such as House Speaker Nancy Pelosi (D-Calif.), endorsed Kennedy.

The win for Markey, who is now heavily favored to hold his seat in November, is the latest victory this year for the increasingly powerful liberals in the party. Markey latched on to his partnership with Rep. Alexandria Ocasio-Cortez (D-N.Y.) — who was more than a decade from being born when he was first elected to the House in 1976 — on their environmental Green New Deal plan to bolster his progressive bona fides.

The liberal wing has scored a series of upsets in House primaries, knocking out decades-long incumbents and claiming open seats.

Markey, 74, faced the 39-year-old Kennedy, who was not yet born when the senator began his political career, and who came into the race with a long list of endorsements. But Kennedy’s message of generational change, which helped power some primary challenges in other states, did not resonate as much as Markey’s focus on his long liberal record and his sponsorship of the Green New Deal.

“We have tremendous momentum,” Markey said in an interview before the campaign’s final push. “The energy level at our events is growing by the day. A lot of it is driven by the climate crisis, by the Sunrise Movement, by young people all across the state who are rallying to this campaign.”

Kennedy, a son of former congressman Joe Kennedy II and grandson of the late Robert F. Kennedy, entered the race as a favorite, after years of expectation that he’d seek higher office. He picked up early support from labor unions, especially the state’s building trades, and he led in early polling.

Kennedy said his family’s name was “invoked more than I anticipated in this race,” and called his family heroes for their devotion to public service.

Kennedy addressed his supporters to concede the race shortly after 10:30 p.m., saying he had built a campaign around people “left behind,” naming some of the working-class towns he had competed in the hardest.

“The senator is a good man. You have never heard me say otherwise,” Kennedy said. “It was difficult at times between us. Good elections often get heated. But I’m grateful for the debates, for his commitment to our commonwealth, and for the energy and enthusiasm he brought to his race.”

Markey won a special election in 2013, replacing then-Secretary of State John F. Kerry, and a full term one year later, but when the race began he lacked the clear name recognition or national following of his predecessors. Since Kennedy was born, just five Democrats have been elected to represent Massachusetts in the Senate. Just one — Markey — never ran for president.

Kennedy raced around the state, accusing Markey of losing touch with constituents, at one point spending 27 straight hours talking to voters, from the day shift to the night shift.

Mary Anne Dube, 76, the chair of Worcester’s Democrats, said she got behind Kennedy immediately, in part because of how much more active than Markey he seemed to be in the state and in part because of the loyalty she placed in his family.

“I’ve been a Kennedy fan, and I think there’s nobody that stands up for the citizens of Massachusetts like the Kennedys,” she said. “It’s wonderful that he’s in. Everything he says is about what we need right now.”

But Markey slowly took control of the race, emphasizing his years of work on climate-change legislation and his working-class roots as the son of a milkman from the blue-collar Boston suburb of Malden.

Some voters questioned why Kennedy would challenge the incumbent when their policy stands were so similar.

“I just think it’s unnecessary,” Sean Dacey, a 44-year-old chef whose restaurant job was eliminated by the coronavirus pandemic, said after seeing Markey speak last week. “I think it might have a bit to do with ambition and looking and seeing an opportunity than with a chance to distinguish himself on the issues. They’re pretty close on the issues. So why bother?”

The Democratic winner of the race will be heavily favored against Kevin O’Connor, who won the Republican nomination.

Markey was boosted by the activist left, including the Sunrise Movement, a network of mostly young people who have demanded a transition to a green economy by 2030. To the surprise of both campaigns, Markey’s unflashy style made him popular online, with a photo of the senator wearing khakis and Nike sneakers going viral, and Ocasio-Cortez (D-N.Y.) endorsing him and touting his work on the Green New Deal.

“As soon as we realized there might be a credible threat to Markey’s reelection we wanted to make sure we were doing everything we could to have his back,” said Evan Weber, the political director of the Sunrise Movement. “We didn’t want to wake up to a bunch of headlines saying Ed Markey lost and voters rejected the Green New Deal.”

The final public polls before primary day found Markey in the lead, dominating Kennedy among younger and more liberal voters.

While Kennedy criticized Markey for his 2002 vote to invade Iraq and his 1994 vote for the crime bill, Markey’s supporters labeled Kennedy a fraud. Tensions boiled over in the final weeks, with Kennedy chastising Markey supporters for goading singers out of a Broadway fundraiser — it was canceled — and a campaign manager decrying the anger and vitriol from ­anti-Kennedy voters online.

“The death threats and assassination references are beyond the pale,” Kennedy’s campaign manager, Nick Clemons, wrote last week. In a letter to Markey campaign manager John Walsh, Clemons demanded that Markey tell his supporters to cease.

Walsh said on Twitter that Markey had condemned “all vile and hateful speech.”

Kennedy’s supporters say that Markey’s needling of the Kennedy name, including an ad in which he said it was “time to ask what your country can do for you” — turning John F. Kennedy’s 1961 inaugural address phrase inside out — gave the congressman an opening by unnecessarily irritating voters who respect the family’s legacy.

Kennedy won the support of Pelosi, who has otherwise avoided intervening in primary challenges, drawing more attention to the race.

Pelosi said Markey’s team had crossed a hallowed line by running a negative campaign against the Kennedy dynasty. “I wasn’t too happy with some of the assault that I saw made on the Kennedy family,” she said, “and I thought, Joe didn’t ask me to endorse him, but I felt an imperative to do so.”

The race became by far Markey’s most expensive primary. According to the campaigns’ ­pre-primary Federal Election Commission filings, Markey spent $10.3 million to fend off his challenger; Kennedy spent $11.5 million. Both candidates were also hit with ads from outside groups, after neither agreed to a truce on super PACs.

As the campaign came to an end, Kennedy argued that voters would see who was more present, who was working harder and who was spending the most time in ignored parts of Massachusetts.

“That’s why I got into this race,” Kennedy said in an interview last week. “Not for generational change, per se. Not as a primary from the left, per se. It’s straight up that you can’t tell me that the planet is on fire, that this is the most urgent moment of our lifetime and then not be here in Massachusetts.”