Rep. Ed Markey, the Democratic candidate for U.S. Senate in the Massachusetts open seat special election, campaigns at Cafe on the Common in Waltham, Mass., Sunday, June 23, 2013. (AP Photo/Michael Dwyer)

With nearly all precincts reporting, Markey beat Gomez 55 percent to 45 percent, thwarting the GOP’s hopes of replicating their 2010 victory in the Bay State, when a pickup truck-driving Republican named Scott Brown rallied Republicans and independents to become the first GOP senator from Massachusetts in 50 years.

“Thank you Massachusetts! I am deeply honored for the opportunity to serve you in the United States Senate,” Markey tweeted Tuesday night.

The race had been an uphill battle for Republicans, but they suggested Tuesday night that Gomez may challenge Markey, a longtime congressman, when the seat is up for election again in 2014.

Tuesday’s special election was to fill the unfinished term of John Kerry, who President Obama tapped to become Secretary of State.

“Today marks the end of the first mile in the marathon to permanently fill the Massachusetts Senate seat,” said Sen. Jerry Moran, R-Kansas, chairman of the Senate’s campaign arm. “Gabriel Gomez is well prepared to win that marathon over the next 16 months.”

Gomez, 47, a businessman and former Navy SEAL, occasionally crept close to Markey in the polls, but never really built the momentum Brown enjoyed in 2010.

In recent days Gomez fell further behind despite Markey’s own tepid approval ratings and the controversy surrounding the time he spends living in Chevy Chase, Md., rather than at home in his Malden district.

Markey, 66, was first elected to Congress in 1976 but has kept a relatively low profile in the House, where he is known mostly for championing green energy initiatives and regulations intended to combat climate change.

He will join Democratic Sen. Elizabeth Warren, elected last year and is now the state’s senior senator. When Markey is sworn in, the Senate will have 53 Democrats and 45 Republicans, along with two independents who caucus with the Democrats.

A Gomez victory was considered a long shot in a state where Democrats outnumber Republicans 3-to-1. But Gomez was hoping to capture the state’s large faction of independent voters, who outnumber Democrats and Republicans combined. Markey’s views were considered very moderate by GOP standards, which also helped him win support in blue Massachusetts.

At one point in recent weeks, Gomez was within single digits of Markey in the polls.

Democrats poured millions of dollars into the race, partly out of fear of a 2010 repeat, and President Obama appeared at Roxbury campaign event with Markey to help rally the Democratic base. Vice President Joe Biden and former President Bill Clinton also campaigned for Markey.

“Even in liberal Massachusetts, he forced Democrats to pull out all the stops as they desperately called in President Obama, President Clinton and Vice President Biden for backup,” Republican National Committee Chairman Reince Priebus said.

House Minority Leader Nancy Pelosi, D-Calif., was the first congressional leader to congratulate Markey Tuesday night.

“Tonight, in electing Ed Markey, the people of Massachusetts chose a leader of vision, a legislator of skill, and a public servant of dignity to represent them in the United States Senate,” Pelosi said in a statement.