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.
Read more about Hong Kong since the handover:
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.
Among those said by the group to have been arrested was Joshua Wong, the leader of the so-called umbrella protest movement.
Slammed to the ground - Juliana Liu, BBC News, Hong Kong correspondent
It was meant to be a routine demonstration by pro-democracy protesters.
The leaders of the League of Social Democrats and Demosisto had gathered to make their way to the square in the Wanchai district where the flag-raising ceremony was taking place. They were demanding greater voting rights for Hong Kong, as well the immediate release of the Chinese dissident Liu Xiaobo.
But without warning, scuffles began between the demonstrators and the police. Faces were slammed to the ground. And then pro-Beijing supporters turned up, waving flags and blaring patriotic music. Each side was shouting abuse at the other.
The tussle went on for more than an hour, with it ending only after the leaders of the pro-democracy demonstration were led away by police, who called it an illegal gathering.
During the ceremony, the flags of China and Hong Kong were raised alongside one another to mark the 20-year anniversary of the city's handover of British rule.
Helicopters flew overhead as onlookers cheered at the ceremony in Golden Bauhinia Square, central Hong Kong.
On Friday, an official protest zone near the convention centre where Mr Xi was guest of honour at an anniversary banquet and variety performance was heavily patrolled, as demonstrators gathered chanting "end one-party dictatorship".
There is growing concern that the Chinese central government is undermining Hong Kong's more politically liberal traditions, despite its promise to give it a high degree of autonomy.
Joshua Wong and 25 other activists were arrested on Wednesday for "breaking the 'public nuisance' law" after climbing into a golden sculpture of a bauhinia flower, Hong Kong's emblem.
The sculpture, which sits by the city's harbour front, was a gift from China and an iconic landmark symbolising the handover.