The Supreme Judicial Court upheld a 1913 state law that blocks nonresidents from marrying in Massachusetts if their marriages would not be recognized in their own states.
The same court legalized gay marriage three years ago, making Massachusetts the first state to do so.
However, in Thursday's ruling, the court said, "The laws of the commonwealth have not endowed nonresidents with an unfettered right to marry. Only nonresident couples who come to Massachusetts to marry and intend to reside in this commonwealth thereafter can be issued a marriage license without consideration of any impediments to marriage that existed in their former home states."
The court argued in its decision that it did not want its state laws to interfere with marriage regulations in other states.
Massachusetts "has a significant interest in not meddling in matters in which another state, the one where a couple actually resides, has a paramount interest," Justice Francis Spina wrote.
The state "can reasonably believe that nonresident same-sex couples primarily are coming to this commonwealth to marry because they want to evade the marriage laws of their home states, and that Massachusetts should not be encouraging such evasion."
Eight gay couples from other states had filed lawsuits challenging the 1913 law. The court sent their cases back to a lower court, saying it was unclear whether those states prohibit gay marriage, according to the Associated Press.
Gov. Mitt Romney praised the ruling, saying, "We don't want Massachusetts to become the Las Vegas of same-sex marriage. It's important that other states have the right to make their own determination of marriage and not follow the wrong course that our Supreme Judicial Court put on us."
Mark Pearsall, one of the plaintiffs and a Connecticut resident, called the ruling "illogical," reported the AP.
"It's a statement that's not really based around any sense of humanity, but really on a sense of politics, which is really not a fair way to treat people. It's a hurtful thing," Pearsall said.