Chinese President Xi Jinping has warned against “impermissible” challenges to Beijing’s authority over Hong Kong.
Mr Xi was speaking at the swearing-in of the territory’s new leader Carrie Lam, as Hong Kong marked 20 years since its handover to China from Britain.
On Saturday afternoon, after Mr Xi had left Hong Kong, thousands of people took part in an annual march calling for greater democracy.
During Mr Xi’s visit there was little opportunity for protest.
An earlier protest had led to clashes with pro-Beijing demonstrators.
Mr Xi’s visit to the city – his first since becoming Chinese leader in 2013 – came amid tight police security.
Several people were detained in the morning, when a small group of pro-democracy activists clashed with pro-Beijing demonstrators close to the site where the lavish ceremony took place.
Organisers said 60,000 people took part in the later pro-democracy march, though police said the figure was much lower.
Heavy rain affected the march, which started at Victoria Park in Causeway Bay. Some protesters carried yellow umbrellas, a symbol of the demonstrations which gripped the city in 2014.
Lam Wing-kee, one of the five Hong Kong booksellers who went missing in 2015 and re-surfaced in detention on the mainland, addressed the march.
The Chinese leader oversaw the swearing in of Ms Lam, the newly-elected chief executive of the territory, along with the rest of her cabinet. She is Hong Kong’s first female leader.
In a speech he said that Hong Kong needed to “improve its systems to uphold national sovereignty, security and development interests”.
“Any attempt to endanger China’s sovereignty and security, challenge the power of the central government… or use Hong Kong to carry out infiltration and sabotage activities against the mainland is an act that crosses the red line and is absolutely impermissible,” he said.
He added that Hong Kong now enjoyed more freedom than ever before.
But while the territory’s Basic Law guarantees wide-ranging freedoms under the “one country, two systems” formula, Beijing’s refusal to grant universal suffrage has triggered sometimes violent unrest.
In Saturday morning’s small-scale protests, pro-democracy party Demosisto said police had arrested five of its members, and four members from the League of Social Democrats.