HONG KONG — A court in Hong Kong ruled on Tuesday that two pro-independence politicians who inserted an anti-China snub into their oaths of office could not take their seats in the city’s legislature, effectively ending a case in which Beijing had taken extraordinary steps to influence politics in Hong Kong.

A judge in the High Court said that Yau Wai-ching, 25, and Sixtus Leung, 30, had “contravened” the territory’s charter and a local law, declaring vacant their seats on the semiautonomous city’s Legislative Council, to which they were elected in September. China’s central government handed down an edict last week that effectively barred them from the council.

Ms. Yau and Mr. Leung, who advocate the city’s independence from China, altered the words of the oath during their swearing-in last month, pledging allegiance to the “Hong Kong nation.” They also displayed a flag in the council’s chambers that bore the words “Hong Kong is not China” and used a term for China that many consider a slur. Their oaths were rejected, prompting the case.

The young politicians’ act of rebellion enraged Beijing. China has ruled Hong Kong, a former British colony, since 1997, under the condition that the city be given a high degree of autonomy for 50 years, including having an independent court system and the right to elect its own legislature.

But the words chosen by Mr. Leung and Ms. Yau — who also used another vulgar term in her oath — alarmed Beijing’s top leaders, for whom any suggestion of secession anywhere in China is anathema and who are tamping down separatist movements in the country’s western regions.

On Nov. 7, China’s Communist Party-controlled legislature took the rare step of weighing in on the court case, with a ruling that officeholders in Hong Kong have to take their oaths “sincerely and solemnly,” with no second chance.

Thousands of people took to the streets in protest on the eve of the ruling, which the local news media had reported was in the works, clashing with the police in scenes reminiscent of the huge pro-democracy demonstrations in 2014. Many Hong Kong lawyers, concerned that the city’s judicial independence was under threat, staged a silent march on Nov. 8.

China’s ruling, an interpretation of Hong Kong’s charter, called the Basic Law, that was issued by the Standing Committee of the National People’s Congress in Beijing, left the Hong Kong court with clear guidance. The city’s pro-Beijing government had asked the court last month to rule that Ms. Yau and Mr. Leung had violated a local ordinance on oath-taking and should vacate their posts.

The interpretation “changed the rules of the game,” said Eric Leung, a law lecturer at the University of Hong Kong.

Image

Sixtus Leung at a protest on Nov. 6. He and Ms. Yau altered the words of their oaths of office during their swearing-in to the Legislative Council last month.CreditIsaac Lawrence/Agence France-Presse — Getty Images

Yet Justice Thomas Au, who made the ruling, said that he would have made the same decision even without Beijing’s extraordinary intervention, and that the two had violated an existing Hong Kong law on oaths and declarations and a provision of the Basic Law, according to a 10-page summary of the decision handed out to reporters. The judge also ruled that the president of the Legislative Council could not let the two retake their oaths.

Mr. Au declared that their seats had been vacant since Oct. 12, the day they delivered their controversial oaths, the contents of which, including the coarse language, were included in the summary. Their names have now disappeared from a directory at the council’s office building. Lawyers for Ms. Yau and Mr. Leung had argued that a court ruling on their tenure as lawmakers violated the separation of powers and that their speech in the legislative chamber was protected and immune from legal action. The court rejected both arguments.

After the decision, Mr. Leung said he planned to appeal to a higher court in Hong Kong. Ms. Yau told reporters that the court’s decision was influenced by Beijing and that it undercut their democratic election. “If the court could strip us of our qualification, we all know what kind of society we live in now,” she said.

Now the focus turns to other elected members of the 70-member council who gave unorthodox readings of their oaths as a form of protest. Unlike Ms. Yau and Mr. Leung, the others either had their original oaths accepted or were given the chance to retake them, raising the legal bar on any attempt to have them removed.

Some pro-Beijing citizens in Hong Kong have asked the courts to use the new interpretation to have other lawmakers removed from office, including Nathan Law, one of the leaders of the 2014 protest movement, and Eddie Chu, who received more votes than any other candidate during the elections. Mr. Law gave a preamble to his oath, saying he could not be loyal to a government that “murders its own people.” Mr. Chu added a statement after he quickly read through his oath: “Democracy and self-determination. Autocracy will die.”

Also unclear is whether Ms. Yau and Mr. Leung will be able to run in a special election to fill their now-vacant seats. The wording of the Beijing interpretation specifies that candidates for office must also take a loyalty pledge, but determining whether that pledge is sincere gives election officials a great deal of discretionary power and is the subject of another legal case being argued in the city’s courts.

A version of this article appears in print on , on Page A6 of the New York edition with the headline: Court in Hong Kong Bars 2 Pro-Independence Politicians From Taking Office. Order Reprints | Today’s Paper | Subscribe