NEW YORK — Sen. Bernie Sanders returned to the campaign trail Saturday with an endorsement from Rep. Alexandria Ocasio-Cortez, making a comeback nearly three weeks after suffering a heart attack with a speech that sought to allay concerns about his health and placed a greater emphasis on the importance of diversity and inclusion.

Speaking under a sunny sky to a crowd of more than 25,000 the largest any Democratic candidate has attracted this yearthe speech showcased for the first time a striking new political alliance between a 78-year-old senator from Vermont fresh off a health scare and a 30-year-old Latina congresswoman from New York who represents a younger and more diverse generation of Democrats.

“I am more than ready to assume the office of president of the United States,” Sanders said after thanking supporters for their best wishes as he recuperated. “I am more ready than ever to help create a government based on the principles of justice.”

“To put it bluntly,” he added, “I am back.”

Saturday’s rally, billed as a “Bernie’s Back” presidential campaign event, was Sanders’s first since suffering a heart attack on Oct. 1 and having two stents inserted to clear a blocked artery. It served to signal that his candidacy, which even some close confidants were unsure would continue in the first days after his health scare, had regained its footing.

Over the past week, Sanders has restored his standing as a force in the fluid Democratic race with a debate performance that won positive reviews, high-profile endorsements and strong fundraising that has expanded a campaign account already flush with cash.

Yet his future in the race has been obscured by lingering questions about his age, health and ability to expand his support beyond a limited, if loyal, base all at a moment when former vice president Joe Biden and Sen. Elizabeth Warren (D-Mass.) are ahead of him in most polls.

Campaign officials hope the addition of one of the party’s biggest rising stars will help him jump-start his campaign headed into a crucial fall stretch. In her remarks, Ocasio-Cortez delivered an impassioned explanation for why she decided to support Sanders, saying “the only reason that I had any hope in launching a long-shot campaign for Congress is because Bernie Sanders proved you can run a grass-roots campaign and win in an America where we almost thought that was impossible.”

She hugged Sanders after he walked onstage. The two then joined hands as the crowd erupted in cheers.

Ocasio-Cortez was part of a parade of introductory speakers, some of whom did not shy away from sensitive topics. The senator’s wife, Jane Sanders, said her husband was healthy and eager to get back to the campaign. Liberal filmmaker Michael Moore dismissed the notion that Sanders’s age is a liability and won cheers for saying that Wall Street would be the one having a heart attack when Sanders is elected.

The Sanders campaign has promised to release his health records by the end of the year. Sanders has not disclosed details about how much damage his heart sustained. And his team waited more than two days to reveal his diagnosis despite persistent questions from reporters, drawing some criticism and questions about transparency that Sanders has said were unwarranted.

Sanders’s anti-establishment message and democratic socialist platform calling for sweeping expansion of the social safety net have made him a rock star on the party’s left. But it has yet to gain traction among a broader slice of Democrats.

Sanders supporters are confident the endorsement by Ocasio-Cortez, an electrifying figure who is part of a youthful coalition of Democrats who rose to power in the 2018 midterms, will help address some of those weaknesses. Sanders also recently won the endorsement of Rep. Ilhan Omar (D-Minn.), who, like Ocasio-Cortez, is a member of an influential group of liberal congresswomen of color known as “the Squad.”

“It’s uniting different generations of Democrats,” said Jonathan Glasser, 61, a South Orange, N.J., resident who attended the event. “And it’s momentum at a time when some people think his campaign is flagging because of his health issues. Him doing so great in the last debate was a really big thing.”

The endorsements surprised many top Democrats who wondered whether they would throw their support to Warren, who is also running as a champion of liberal ideas, or stay neutral. But behind the scenes, Sanders worked diligently to secure their support, an effort that paid off at a moment he needed most.

Bryan Charlot, a 25-year-old from Manhattan, said he felt Sanders had gotten an unfair reputation that Ocasio-Cortez’s endorsement could remedy. “Everybody thinks he hates women, so to have a prominent female voice out there on the trail for him will be good,” Charlot said.

Sanders wrote his Saturday speech himself, aides said, honing it over the past couple of days. While he hit many of the populist themes he has long talked about, such as universal health care, he also touched on inclusivity, calling on supporters to fight for the needs of others.

“Find someone you don’t know — maybe somebody who doesn’t look like you,” Sanders told the crowd as he wrapped up his remarks. “My question now for you is, ‘Are you willing to fight for that person’ ” as hard as you fight for yourself?

Sanders also accused some unnamed Democrats of believing “that real change is not possible,” including some rivals he debated Tuesday night.

While Sanders chose to make his return to the campaign trail in New York, he and his top aides are focusing heavily on another state. Campaign officials said Iowa, which kicks of the nomination contest on Feb. 3, is the priority in the coming weeks. The campaign intends to extend its current television ad buy there, and Sanders plans to kick off an “End Corporate Greed Tour” in a few days.

In 2016, Sanders fought Hillary Clinton to a near draw in Iowa. But recent polls have shown him trailing Warren and Biden.

Saturday’s rally had a festive atmosphere. One man came dressed in an oversize Sanders mask and held up a sign that said “Enough i$ Enough!” The crowd broke into chants of “We will win!” The distinct anti-establishment atmosphere that has been evident at Sanders’s events this year was clear as speakers railed against large corporations to cheers. Outside, a handful of protesters waved Trump 2020 flags.

For some attendees, it was also an emotional moment. Few, if any, politicians have cultivated a core following as devoted as the one Sanders has built since his emergence on the national stage as a presidential candidate four years ago. His heart attack was a shock for many of them.

“I think, if anything, this is going to give him extra focus and more,” said Alice Mosdell, 68, as she started tearing up. Her friend patted her on the back and finished her sentence for her. “Stronger than ever,” said Maria Concilio, who wore a “Feel the Bern” Christmas sweater. “And ready to make a difference,” added Mosdell, who came from Millburn, N.J., wearing a Sanders T-shirt.

Some in the crowd said they were not worried about any lingering effects of Sanders’s heart attack. One sought to compare him favorably to Trump, who is 73 and has faced questions about his own health.

Campaign officials and allies said the developments of the past week have created the feel of a fresh start that has boosted morale, renewed confidence, and helped the team move past the most difficult and uncertain phase of the campaign.

Thanks to a huge network of small-dollar donors, Sanders started October with $33.7 million in his campaign account, a hefty sum that ensures he will have the resources to compete in early states and is not in any danger of running low on cash in the near future.

Still, questions remained about what tack Sanders will take in the months ahead. He has sent mixed signals about how aggressive his campaign schedule will be, after running at a breakneck pace for months. And at key times, he has declined to take the advice of his confidants, advisers and friends.

Some of them are eager to see him sharpen the contrast that he has drawn with Biden and hear him speak more openly about his personal life, including his heart attack. On Saturday, Sanders seemed to heed their input, framing his health situation as a way to talk about what he says are severe economic, social and health-care issues facing people every day.

“There is no question that I and my family have faced adversity this last couple of weeks, but the untold story is that people” across the country are facing their own struggles, he said.

Sanders and his surrogates used the setting — a park across the river from Manhattan high-rises and across the street from the largest housing project in the country — to highlight the effects of income inequality and the reasons various government programs do not received their prescribed funding.

“It is an outcome of a system that devalues the poor, that devalues working people, and that prioritizes buildings like those over buildings like these,” Ocasio-Cortez said, as she pointed to the skyscrapers to her right, then the apartment buildings to her left, to make her point.

The campaign said 25,872 attended the rally, which included people packed against the fences around the park. Sanders began his remarks with an apology to those who had to settle for spots on along those fences. His campaign had obtained a permit for only 20,000, he told them.