HONG KONG — Police used force early Tuesday to clear thousands of protesters in and around Hong Kong’s Legislature after some broke into the complex and occupied it Monday, the 22nd anniversary of the semi-autonomous city’s return to Chinese rule.

The escalation has brought Hong Kong into unprecedented and uncertain territory, and represents the biggest test of Beijing’s grip over the global financial hub and the status under which it operates.

Protesters on Monday smashed their way through metal barricades and glass doors surrounding Hong Kong’s Legislative Council. As they wrote graffiti on walls, tore down portraits of pro-Beijing officials and emptied rooms of chairs and desks, the mostly young protesters escalated weeks of tensions and massive demonstrations here to a new level.

The demonstrators occupying the complex penned a declaration that included a call for overthrowing the “puppet Legislative Council and the Government,” and they vowed to stay. But just after midnight Tuesday, police equipped with riot shields, tear gas and other projectiles began ejecting protesters from streets surrounding the complex, sending them fleeing. Police then retook the complex, stopping and frisking the young protesters who remained nearby.

More than 500,000 demonstrators, meanwhile, marched peacefully across the city Monday and forced major thoroughfares to shut down.

The scenes of defiance were the latest indication that anger here, sparked by plans to allow extraditions to China but now incorporating broader concerns about Hong Kong’s autonomy and Beijing’s influence, will not be easily quelled.

The protesters smashed shutters, broke windows and ripped down metal fencing around the Legislative Council, eventually forcing their way into the building. Protesters repeatedly tried to slam against metal shutters and pry them open as riot police stood guard.

At some point during the night, police appeared to vacate their posts. By 9:30 p.m., dozens of demonstrators wearing yellow hard hats and carrying umbrellas had entered the building and were roaming the complex. Outside, protesters cheered as more windows and doors were smashed open.

Later Monday night, police said the building was “violently attacked” and “illegally entered.” In a tweet, they warned that they would conduct a sweep with “reasonable force” and urged people to leave the area.

The Hong Kong government in a statement also condemned the “violent acts,” which it said was the work of “radical protesters.”

Protesters inside the building, however, vowed to come back even if they were cleared out.

“Unless universal suffrage and a just election system are in place, we shall never stand down,” they said in a statement.

Monday’s chaotic demonstrations came on a day when the territory’s return to Beijing is officially celebrated.

Before dawn, riot police and hundreds of protesters gathered on roads leading to a square where the Hong Kong and Chinese flags were set to be raised.

The ceremony, which was attended by Hong Kong leaders and dignitaries including Chief Executive Carrie Lam, was moved indoors as crowds of protesters gathered.

Officials said the event, which has never been held indoors, was moved because of “inclement weather.”

As helicopters carrying the flags flew over, protesters on the streets below waved middle fingers at them. Earlier in the morning, protesters had replaced a Chinese flag with a black flag featuring a withered Bauhinia flower, a riff on the Hong Kong flag. That flag was still flying on Monday night.

Hospitals and police have not confirmed the number of injuries from the clashes. Police said in a statement that protesters earlier Monday had pelted officers with objects containing an “unknown liquid,” which made their skin swollen and itchy. Thirteen officers were treated at hospital and discharged.

July 1, the anniversary of the 1997 handover of sovereignty, has always been marked by marches featuring hundreds of thousands of people who want to uphold Hong Kong’s unique status, democratic characteristics and relative freedoms compared with mainland China.

But after weeks of unprecedented tensions in the territory, Monday’s protests took on a different flavor. In the face of an increasingly assertive Beijing, protesters saw the occasion as their final chance for a massive stand against a government they believe is not working in their interests.

An hour into the planned afternoon march, police sent out a warning, discouraging people from joining the procession.

“Police absolutely respect people’s freedom of assembly, procession and expression of opinion in a peaceful and orderly manner,” the statement said. “However, Police’s risk assessment indicates that there is a serious safety threat.”

Yet, demonstrators turned up in the tens of thousands, filling Hong Kong’s main roads with a swell of shuffling people once again.