John Kerry’s former Senate seat will stay in Democratic hands as Rep. Ed Markey defeated Republican Gabriel Gomez in Tuesday’s special election, according to the Associated Press.

The win is an important one for Democrats who now retain a 54-46 margin in the Senate, with a chance to gain back one more in an October special election in New Jersey.

“I’m going to the U.S. Senate to stand up for you, and for the values that I’ve always believed in: honesty, fairness and equality,” Markey said during his victory speech Tuesday night. “I pledge you, that I will be a senator who will fight for you, every hour, on every vote, on every issue to make progress for you and your families.”

Since winning the nomination in April, Markey, a 37-year congressional veteran, held the upper-hand in the heavily-Democratic state. But his party took no chances after having been burned just two years ago. In a January 2010 contest to fill the seat of the late Ted Kennedy, Republican Scott Brown shocked the political world by riding a rising GOP wave and discontent for health care reform to victory.

Still sore from that defeat, Democratic groups invested heavily to boost Markey, combining to spend more than $5.2 million on his behalf, compared to the $3 million spent by Gomez and an outside super PAC on his behalf.

And Democrats spared no surrogate, with President Obama, Vice President Joe Biden, First Lady Michelle Obama, and former President Bill Clinton all trekking to the Bay State to stump for Markey. Arizona Sen. John McCain appeared on behalf of the GOP nominee, as did Brown, who held an election eve rally for Gomez.

In a memo Tuesday, the Democratic-aligned Senate Majority PAC, which spent over $1 million on TV ads for Markey, said they “began working late last year with numerous partners, in Massachusetts and nationally, to prevent another sneak attack that allowed Scott Brown to win in the January 2010 special election.”

Only Americans for Progressive Action, a new GOP-aligned super PAC backed by a wealthy California winemaker, came to Gomez’s aid, spending over $700,000 on ads. But in the end, it wasn’t enough, and Gomez campaign strategists were openly frustrated that other cash-laden outside groups, such as Karl Rove’s group, American Crossroads, had sat on their hands.

In his concession speech Tuesday night, Gomez said his campaign “went up against the machine” and was “massively outspent.” But he also said he would make no excuses for his loss and that both he and Markey are better people for having gone through the contentious race.

Some national Republicans saw Gomez as just the type of candidate the GOP needed to push as they tried to expand their base after a disappointing 2012 election cycle–a young, moderate Hispanic veteran and businessman. In debates and on the campaign trial, Gomez underscored key points where he differed with his party, including on climate change, support for same-sex marriage and expanded background checks for gun purchases.

Gomez also argued he could be a key bipartisan broker in the immigration debate, boasting Tuesday morning on The Daily Rundown that he wanted to make the working reform group the “Gang of Nine.”

While Gomez tried to put some distance between himself and the GOP, Markey continually countered that Gomez would give the Republicans another vote. The Democrat highlighted Gomez’s opposition to an assault weapons ban. Though Gomez, a Catholic, says he’s personally anti-abortion, he has said he wouldn’t work to overturn existing law. But Markey argued that Gomez would vote to confirm Supreme Court justices who would overturn Roe. Gomez, a wealthy former investment banker, also faced criticism over a quarter-million dollar tax break he claimed on his home.

In the end, lightening didn’t strike twice–at least not in Massachusetts. The economy is improving, health care is being implemented, and the state’s Democratic and independent voters had no apparent desire to reject their party in Washington or send a message to President Obama.

Many Democrats privately grumbled that Markey didn’t have the most rigorous campaign schedule and wasn’t the most exciting candidate. Still, the dean of the state’s congressional delegation is now headed to the Senate. He wasn’t great but, the state’s voters made clear, he was good enough.

NBC’s Andrew Rafferty contributed to this report.