The first elite runners crossed the finish line late Sunday morning at the TCS New York City Marathon while thousands more participants were miles behind on the five-borough race that took place under unprecedented security measures less than a week after a terrorist attack in lower Manhattan.

“We are not giving in to terrorism,” Mayor Bill de Blasio said on WABC/7 at the start of the run in Staten Island. “We are going to show they can’t change our democracy, they can’t change our city.”

More than 50,000 marathoners were following the 26.2-mile route as some vied for their personal best while others hoped to just finish the race, in its 47th year.

Shalane Flanagan, 36, of Oregon, won the women’s competition with an unofficial time of two hours, 26 minutes, and 53 seconds. Flanagan is the first American woman to win the race since 1977 when Miki Gorman won with a time of 2:43:10.

Geoffrey Kamworor, 24, of Kenya, won the men’s race in two hours, 10 minutes, and 53 seconds.

Runners from all over the world poured into Fort Wadsworth on Staten Island for an 8:32 a.m. start with the professional wheelchair division. Those racers were followed by handcyclists and other disabled athletes, then elite runners and, minutes before 10 a.m., the first of the recreational runners. The last wave took to the course around 11 a.m.

With more than 120 countries represented at the race, de Blasio said the marathon was a “life-affirming” event.

“Look at that unity,” he said. “This race is one of the best examples to all the rest of the world about what’s great about New York City and America. Here’s everyone together in a common cause. And it’s a beautiful thing.”

Spectators, projected to number 2 million, lined the streets for the world’s largest marathon, which crosses the Verrazano-Narrows Bridge and then runs mostly north through Brooklyn, Queens, Manhattan and the Bronx before turning west back toward Manhattan and then south to the finish line in Central Park.

Crowds started to thin in some areas by the afternoon as rain moved into Manhattan, soaking the finish line. But the early afternoon drizzle didn’t stop First Avenue on the Upper East Side from taking on a carnival atmosphere as spectators cheered the runners and bar patrons bearing plastic cups of beer spilled out onto the sidewalk.

“It’s all about the energy from the crowds, like on Fifth Avenue and near [Central Park],” runner Rebecca Schwartz, 37, of Bergenfield, New Jersey, said before the race. “It’s special. That crowd keeps you going.”

Flags from Sweden, Germany, Spain, Ireland, Argentina, Greece and other nations flew along the race route, and handmade signs urged the runners on. Every few blocks live music picked up where cowbells and whistles left off.

Kaitlin Pearse, 27, of Kings Park, ran her fourth marathon, breaking last year’s record that qualifies her for the Boston Marathon.

“I can’t believe it,” said Pearse, who started running for Team in Training, a global marathon group that raises money for leukemia research around the world. “Last year we raised $1.5 million. We absolutely broke that record this year. There are hundreds in the Team In Training,” she said.

Pearse’s father, Rich, 57, was diagnosed with Hodgkin lymphoma in 2014. He is now in remission after a stem cell transplant at Stony Brook University Hospital, but Pearse said she will keep running to raise money for medical research.

“My father’s in remission and I run for him,” said Pearse.

Security efforts along the course included barriers on both sides of the roads and parked police and sanitation vehicles at various points to act as blocker trucks in the event of an attack. On Tuesday, a man drove a rented truck along a lower Manhattan bike path, killing eight people and injuring 12 others.

Officers were posted at many corners and subway entrances, and officials said countersniper teams were in place along the route. Law enforcement officials said they also had heavy weapons teams, aviation units and plainclothes officers in place for the duration of the race.

“I’m in the safest place in the world,” said Denise Morgan, 49. “And that’s because a lot of people here are doing their job.”

The Baldwin native has been volunteering at the marathon with the New York Road Runners for 25 years. She lives in Connecticut now, but makes the trip each year to volunteer at water stations for the nonprofit running organization.

She said she was worried about security “for a minute or two” before deciding to “suck it up and not let them win.”

Tim Roven, 49, of Boerum Hill, said he wasn’t deterred by the recent terrorist attack.

“It’s on my mind, but . . . I’m coming no matter what,” he said.