House Speaker Nancy Pelosi (D-Calif.) endorsed Rep. Joe Kennedy on Thursday in the increasingly bitter Senate Democratic primary in Massachusetts, abandoning her longtime ally Sen. Edward J. Markey a few days after he lodged attacks on the iconic family dynasty.

Pelosi cited Kennedy’s hard work in campaigning for many of the Democrats who won in 2018, flipping the majority and returning her to the job of speaker, but she also cited her own family’s close ties to the Kennedys, including her father’s role running the Maryland campaign for John F. Kennedy’s presidential bid in 1960.

“I became close to the Kennedy family from then on,” she said in an exclusive interview with The Washington Post Live team, noting that she previously served in the House with Joe Kennedy’s father and his cousin, former congressman Patrick J. Kennedy (D-R.I.).

House Speaker Nancy Pelosi (D-Calif.) endorsed Rep. Joe Kennedy (D-Mass.) in the Senate Democratic primary instead of incumbent Sen. Edward Markey (D-Mass.). (Washington Post Live)

Pelosi said that Markey’s campaign 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 told The Post’s opinion columnist Karen Tumulty, “and I thought, Joe didn’t ask me to endorse him, but I felt an imperative to do so.”

In an official statement, Pelosi, 80, signaled that it was time for a new crop of leaders who would represent “this party’s future,” a shot at Markey, 74, who has been in Congress for 44 years.

Kennedy, 39, serving his fourth House term, was just a couple months old when Markey was sworn into his third term in Congress in 1981.

At a campaign stop Thursday, Markey declined to criticize Pelosi for endorsing his rival.

“Nancy Pelosi is a great leader for our country,” Markey said after an appearance in Worcester, Mass., before boarding his campaign bus.

Later, he issued a statement praising his friend Pelosi. “Any candidate would be proud to have her endorsement. I congratulate Joe Kennedy,” he said.

The primary is Sept. 1, with the winner expected to coast to victory in the heavily Democratic state in the general election.

From her first days in Congress, back in 1987, Pelosi allied herself with Markey and a crop of rising Democratic stars who were agitating for greater power within a House that was still run by old, powerful committee chairmen.

When she first became House speaker, Pelosi undercut one of those “old bulls,” then-Rep. John D. Dingell (D-Mich.), by creating a special committee on climate change because she believed Dingell was too friendly with the auto industry. She named Markey as its chair.

In 2013, Markey followed other friends of the 1980s House clique, such as Sens. Charles E. Schumer (D-N.Y.) and Richard J. Durbin (D-Ill.), into the Senate.

But by early 2019, realizing he was headed for a potentially tough primary as an older White man in an increasingly diverse party, Markey drew an alliance with Rep. Alexandria Ocasio-Cortez (D-N.Y.) over climate legislation that they called the Green New Deal.

Pelosi had long considered the environment a cornerstone issue of her time as speaker but has been dismissive of their proposal for lacking concrete legislation that can be implemented into law.

Ocasio-Cortez, who has broken ranks with Pelosi to endorse primary challengers this year to incumbent Democrats, lashed out on Twitter over the speaker’s decision to buck an incumbent in a Senate race.

“No one gets to complain about primary challenges again,” she tweeted, noting that Pelosi supported cutting off financial ties to firms that worked against House Democratic incumbents in primaries.

Still, as this Senate race drew more contested, Pelosi remained neutral, actually donating to both the Markey and Kennedy campaigns.

In recent days, however, Markey has tried to run as the insurgent candidate, channeling support and energy from Ocasio-Cortez, who rose to fame with a stunning upset of 20-year incumbent Joseph Crowley (D-N.Y.) in the 2018 primaries.

Markey released a powerful three-minute Web video that, according to a Pelosi aide, the speaker took as an attack on the Kennedy family and its storied place in Democratic politics.

In the video, Markey turned John F. Kennedy’s famous 1961 inaugural address inside out.

“With all due respect, it’s time to start asking what your country can do for you,” Markey said.

Earlier this week Kennedy stood with local Black leaders in Boston and, for more than eight minutes, delivered an impassioned defense of his family’s legacy and cited votes from Markey’s early years in Congress that do not comport with today’s Democratic Party, including voting with Republicans in 1981 on a tax amendment that was considered a proxy for opposing interracial marriage.

“Senator Markey is trying to convince you that he is going to do something different in the next six years than he has done in the last 47, he’s asking you to trust that he can lead a movement that he has never been a part of. He’s telling you that he deserves a mantle that he has not earned,” Kennedy said at the news conference.

Public polls, which initially gave Kennedy a large lead, have shown Markey surging, possibly to a narrow lead.

Dave Weigel in Worcester, Mass., contributed to this report.