Shalane Flanagan dethroned Mary Keitany on Sunday and became the first American woman to win the New York City marathon since 1977, potentially ending the 36-year-old’s decorated career with her first major marathon victory.

Keitany had won three straight New York Marathons, but Flanagan pulled away from the Kenyan with about three miles to go. Flanagan finished with an unofficial time of two hours, 26 minutes and 53 seconds, about a minute faster than Keitany. Geoffrey Kamworor of Kenya held off countryman Wilson Kipsang to win New York Marathon, his first major marathon win.

Flanagan cried and yelled as she approached the finish line. The last American woman to win New York was Miki Gorman, who won consecutive titles in 1976-77.

“It’s indescribable,” Flanagan said. “It’s a moment I’m trying to soak up and savor.”

Flanagan finished second in New York in her first marathon in 2010 but hadn’t run the race since. After a fracture in her lower back kept her out of the Boston Marathon, Flanagan trained hard for New York with an eye on Keitany. Flanagan called Keitany “the alpha racer’’ and said she was ready to “suffer dearly’’ while keeping up with the unpredictable Kenyan.

Flanagan had said she may retire if she won New York. During a distinguished career she appeared in two Olympic marathons, finishing sixth in 2016. She won bronze in the 10,000m at the 2008 Games, and was later upgraded to silver after Turkey’s Elvan Abeylegesse was stripped of her medal for doping violations.

In the men’s race, Kamworor edged Kipsang with a time of 2:10:53. Kamworor separated from the pack late and seemed like he would cruise to his first major marathon victory, but Kipsang appeared on his heels in the final stretch. Kipsang didn’t have enough to catch Kamworor, though. The 24-year-old winner kissed the pavement right after crossing the finish, then turned to embrace Kipsang.

The race came less than a week after a terrorist attack in the city left eight people dead. “New York City said strongly we’re not giving into terrorists,” New York mayor Bill de Blasio said, adding, “That’s a message we’re sending the whole world.”

