President Obama on Wednesday joined a parade of top Democrats rallying support for the Senate campaign of Rep. Edward J. Markey, attempting to ensure they do not lose another critical race in a Massachusetts special election.

With the surprise 2010 victory of Republican Scott Brown still seared into their memories, Democrats have seen Markey’s lead narrow ahead of a June 25 special election to fill the seat previously occupied by Secretary of State John F. Kerry.

Markey, a House member for more than 35 years, is facing a surprisingly tough challenge in this Democratic-friendly state from private equity investor and former Navy SEAL Gabriel Gomez, who is promoting himself as the new face of the GOP.

Obama visited a local sandwich shop and rallied a thunderous crowd early Wednesday afternoon, saying he needs allies such as Markey in the Senate to stand up to Republicans who want to roll back his health-care overhaul and economic policies.

“The fact of the matter is that a whole bunch of Republicans out there are not interested in getting things done. They think compromise is a dirty word,” Obama said at the Reggie Lewis Track and Athletic Center in the Roxbury Crossing neighborhood. “We’ve got to have some Democrats, like Ed Markey, who will stand up and do the right thing. That’s what we need.”

Obama’s appearance followed a high-profile Markey fundraiser in Washington on Tuesday night that featured Vice President Biden and former vice president Al Gore. On Saturday, former president Bill Clinton is planning a campaign visit.

Brad Dayspring, communications director of the National Republican Senatorial Committee, said the legion of high-profile Democrats working on Markey’s behalf shows the growing desperation of his campaign.

“I think one of the interesting ways to gauge a campaign is the actions of your opponent. Over the last 10 days, Democrats have shown their concern about the race,” Dayspring said. “They wouldn’t be sending the money and big names if they weren’t very concerned.”

Justin Barasky, press secretary for the Democratic Senatorial Campaign Committee, said his party is confident that Markey will win. He said the visit by the president — as well as the national party spending money on television ads and a recent fundraising appeal by freshly elected Sen. Elizabeth Warren (D-Mass.) — are all happening “because we’re taking nothing for granted.”

A new poll this week by Suffolk University showed Markey leading Gomez 48 percent to 41 percent, down from a 52 percent to 40 percent edge last week. Special elections are notoriously difficult to poll, but Biden was forthright about the challenges facing the Markey campaign at the fundraiser Tuesday.

“Barack Obama’s not at the head of the ticket. And that means those legions of African Americans and Latinos are not automatically going to come out,” Biden said at the fundraiser. “No one has energized them like Barack Obama. But he’s not on the ticket. So don’t take this one for granted.”

Obama made a similar appeal in his remarks at the athletic center in Boston.

“This election is going to come down to turnout,” he said. We’ve got a whole lot of Democrats in this state and a whole lot of Obama voters, but you can’t just turn out during a presidential election. You’ve got to turn out in this election.”

Obama’s visit to Massachusetts, followed by an evening trip to Miami for two Democratic fundraisers, is part of a concerted attempt by Obama to help preserve the Democratic majority in the Senate in 2014.

It will be a tough challenge. Since the Eisenhower administration, the party in control of the White House for a second term has lost an average of six Senate seats in the midterm elections.

Seven of the Democratic Senate seats up for grabs in 2014 are in states won last year by GOP presidential nominee Mitt Romney; Republicans only have two seats to defend in states won by Obama.

“The map in 2014 doesn’t look that good for Democrats. because they have to fight a lot of these races in Republican turf,” said Jennifer Duffy, senior editor for the Cook Political Report.

A Democratic campaign official conceded that it will be difficult to enlarge the Democratic margin in the Senate, which is six seats, including two independents. “A win for us this cycle is about holding the majority,” the official said.