HONG KONG — Hong Kong's highest court ruled in favor of the government Monday in upholding its use of a colonial-era law to unilaterally ban masks at the height of protests last year, reversing a lower court's ruling.

The ruling represents a defeat for Hong Kong’s pro-democracy opposition, now forced out of the legislature, which tried to establish that the ban was unconstitutional and violated basic liberties. It also upholds the power of Hong Kong’s chief executive, who is handpicked by Beijing, to use ­colonial-era security laws to unilaterally enact legislation.

Five judges on the Court of ­Final Appeal unanimously ruled that the ban on face masks in October 2019, enacted when anti-government protests were raging on the streets of Hong Kong, was proportionate and necessary. The 71-page judgment also detailed the actions of the protesters, highlighting the violence, “unlawfulness” and “vandalism” prevalent last year, using those reasons to uphold the ban on masks.

“The interests of Hong Kong as a whole should be taken into account, since the rule of law itself was being undermined by the actions of masked lawbreakers,” the judgment read, adding that with their faces covered, protesters “were seemingly free to act with impunity.”

Unrest broke out in the city in June 2019, sparked by a proposal to allow extraditions from the city to the Chinese mainland. Under the terms of Hong Kong’s 1997 hand­over from Britain to China, the city is meant to enjoy its way of life, an independent judiciary, the ability to protest and other basic rights until at least 2047.

The protests spiraled into a full-blown rebuke of the Chinese Communist Party and its grip on the city. The mask ban in fall 2019 fueled more protests and another wave of violent anger on the streets. In late June 2020, Beijing passed a national security law in Hong Kong, its solution to ending anger on the streets by outlawing dissent. Under that law, broadly worded crimes such as “secession” and “foreign interference” can be punished by up to life in prison.

The court judgment had the effect of backing the government’s narrative of the protests last year, painting the demonstrators as out-of-control mobs that had to be subdued by all means.

“The judgment privileges one particular narrative of the events of 2019 — that of violent, out-of-control lawlessness — over any other,” said Antony Dapiran, a lawyer who wrote a recent book about Hong Kong’s protests. “Without any additional context, it comes to a conclusion which supports the government’s response.”

In November 2019, Hong Kong’s Court of First Instance overturned the ban, ruling it unconstitutional and prompting an outcry from Beijing’s representatives in the city. The legal tussle that followed came at an awkward time, when the government was making mask-wearing mandatory because of the coronavirus, which was first detected here in January.

The Court of Appeal ruled in April that the ban was partly unconstitutional and that masks should be allowed at legal public gatherings.

Monday’s ruling comes as the courts are under intense scrutiny and are considered the last protection against Beijing’s efforts to overhaul Hong Kong’s institutions. Last month, British Foreign Secretary Dominic Raab said he would consider pulling British judges from the Court of Final Appeal, which handed down the Monday ruling. About 13 foreign judges are non-permanent members of the court under Hong Kong’s mini-constitution calling for the observance of common law traditions from the colonial era.