(John Ferrarone/AP)

Rep. Edward J. Markey (D) won the Massachusetts Senate race Tuesday, defeating Republican Gabriel Gomez with the help of ample reinforcements from national Democrats looking to avoid a repeat of Republican Scott Brown’s 2010 upset victory.

With nearly all precincts reporting Tuesday night, Markey led Gomez 55 percent to 45 percent, according to the Associated Press, which called the race about an hour after polls closed. Markey’s special election win will keep 54 senators in the Democratic Caucus, compared to 46 in the GOP Conference. The longtime congressman and dean of the state’s U.S. House delegation will fill the seat once held by Secretary of State John F. Kerry.

The win comes nearly three and a half years after Democrats hit bottom in deep blue Massachusetts. Republican Scott Brown defied the odds in early 2010 to snatch a seat from Democratic hands when he defeated Attorney General Martha Coakley (D) in a special election to fill the Senate seat once held by the late Edward M. Kennedy.

Democrats resolved early not to be caught off guard this time. Markey enjoyed robust support from allied groups and surrogates throughout his campaign. President Obama and Vice President Biden each traveled to Massachusetts to campaign with him in the closing weeks. The Democratic Senatorial Campaign Committee and Kerry endorsed his candidacy a day after he announced it. And a Democratic super PAC made a seven-figure investment on television ads.

“We began working late last year with numerous partners, in Massachusetts and nationally, to prevent another sneak attack that allowed Scott Brown to win,” said Craig Varoga, a senior strategist at Senate Majority PAC, a Democratic group.

Democratic Senatorial Campaign Committee Executive Director Guy Cecil wrote in a memo that "the lesson from Scott Brown’s accidental win in 2010 was that Democrats must never take a race for granted."

Turnout was anticipated to be very low on Tuesday, an oppressively hot day in the Bay State. The race attracted little attention over the last few months, and few voters tuned into the televised debates between Markey and Gomez.

Gomez drew some early comparisons to Brown, but it became clear late in the race that he wasn't going to repeat his success. The first-time candidate, a former Navy SEAL and the son of Colombian immigrants, cast himself as an independent voice with an outside-the-beltway perspective. But he never really broke through; polls showed him trailing Markey throughout the race.

Many Republican groups and donors were not convinced Gomez was worth a major investment. The National Republican Senatorial Committee says it spent nearly $1 million on the race, but American Crossroads and Crossroads GPS, GOP-aligned groups that often enter the mix in Senate contests, were nowhere to be found. Nor was the Chamber of Commerce. And one pro-Gomez super PAC didn't raise very much money.

The decision to remain on the sidelines could set off a new debate in GOP circles about how best to deploy resources, coming off a tough cycle for Republicans, who lost some key Senate races in red states last year. During the campaign, Gomez allies publicly urged Republicans to join his cause.

“Do conservatives have nerve to try & win?” tweeted Gomez campaign consultant Brad Todd in late May. “Will [the] mod[erate] R donor class put its money where mouth is? Is this a party or therapy session?”

Republicans must pick up five seats to win back the Senate majority this cycle. That number is expected to climb to six later this year when New Jersey voters elect a replacement for appointed Sen. Jeff Chiesa (R-N.J.). Newark Mayor Cory Booker (D) is the front-runner in the blue state race, in which Republicans were unable to land a major recruit.

In a memo to supporters, National Republican Senatorial Committee Chairman Jerry Moran (R-Kan.) pointed to the fact that Democrats had to spend big, despite the state's Democratic-tilt. And he signaled that Gomez could be back.

"Today marks the end of the first mile in the marathon to permanently fill the Massachusetts Senate seat. Gabriel Gomez is well prepared to win that marathon over the next 16 months," Moran said.

Nearly four decades after winning his first congressional campaign, Markey campaigned for the Senate as a champion of liberal values. He ran to the left of fellow Democratic Rep. Stephen F. Lynch in the primary. In the general election, Markey sought to tether Gomez to Senate Minority Leader Mitch McConnell (R-Ky.) throughout the course of three televised debates.

Markey repeatedly slammed Gomez over his stance on guns, women’s issues and entitlement spending. His campaign also raised questions about the Republican's work in the private equity sector and criticized Gomez for a tax break he took on his home.

For his part, Gomez sought to pitch himself as a moderate alternative to Markey and contrast his career outside politics with Markey’s decades-long tenure in the House. “You are the poster boy for term limits,” Gomez told Markey at their first debate.

Brown helped Gomez behind the scenes. But he didn't campaign in person with Gomez until hours before polls opened.

Markey will replace Sen. Mo Cowan (D), whom Gov. Deval Patrick (D) appointed as an interim replacement for Kerry. A close Patrick confidant and former chief of staff to the governor, Cowan is the only black Democrat currently serving in the Senate.

Markey won’t have long to celebrate his win before he must return to campaign mode. He will face reelection in November of 2014, when he will be vying for a full six-year term.

Updated at 10:09 p.m.