Hanes said he was disappointed.
"The decision to issue a marriage license was not made in haste, and what we're going to do next, based on this decision from the court, will not be made in haste," Hanes said.
It was not immediately clear what the decision will mean for the 174 couples who obtained licenses.
The state Health Department under Republican Gov. Tom Corbett took Hanes to court after he began issuing licenses to same-sex couples in July, despite a 1996 state law that defines marriage as between a man and a woman. The department argued that Hanes' actions could create chaos.
James Schultz, Corbett's general counsel, issued a statement saying the key issue was whether local officials can decide which laws to uphold or reject, based on their personal legal opinion.
"We respect the interests and dignity of all the parties involved in this case, but we are a government of laws and it is important that all office holders across the state enforce those laws uniformly," Schultz said.
A separate challenge to Pennsylvania's same-sex marriage ban is pending in federal court.
Robert Heim, a lawyer for some of the same-sex couples who obtained licenses from Hanes, noted that Pellegrini said the legality of the licenses was not an issue before him.
"The 32 couples that I represent are going to have to decide whether they also want to litigate it in the Commonwealth Court, since Judge Pellegrini virtually invited it," Heim said.
In his opinion, Pellegrini said, "There are no obstacles preventing those adversely affected by the provisions of the Marriage Law," such as the 32 couples, "from asserting their own rights in an appropriate forum," and he cited the pending federal lawsuit as an example.
Nicola and Tamara Cucinotta of Paoli filed a lawsuit in Commonwealth Court last Friday against the governor. The suit argues that their marriage is permissible under the state constitution and asks the state to recognize their union.
The women obtained one of the first same-sex licenses from Hanes this summer, and later married.
"It doesn't say anything about Hanes, or about Montgomery County, or whether they received a license or (not)," lawyer Cletus Lyman said. "It says they are suing for the right to be married."
Hanes, a Democrat and an elected official whose duties include marriage licenses, said the law conflicts with his constitutional obligations. His actions followed the U.S. Supreme Court's decision to throw out part of the federal Defense of Marriage Act, and a statement by Pennsylvania Attorney General Kathleen Kane that the same-sex marriage ban was unconstitutional.
Kane is not defending the federal challenge to the same-sex marriage ban, having turned over that case and the Hanes matter to Corbett's lawyers.
ACLU of Pennsylvania attorney Vic Walczak said Pellegrini's decision will have no impact on the federal case.
"It is full speed ahead for the ACLU lawsuit," Walczak said.
Mattthew Mionczynski, 54, and David Soptick, 56, of Norristown, picked up a marriage license on Wednesday afternoon, amid concerns the court would soon rule against Hanes. They have been together for 17 years and are both now retired.
"We're still going to get married, and then let them challenge it," Mionczynski said. "We got the license from the courthouse. It should be legal. I don't care what they ruled."
Pennsylvania is the only northeastern state that does not grant legal status to marriage or civil unions between individuals of the same sex.