Xi spoke in a televised address after swearing in new Hong Kong leader Carrie Lam, who is already being cast by critics as a China stooge in a city where many are angry at Beijing's tightening grip on the freedoms of its nearly eight million people.
A huge security operation shut down large parts of Hong Kong for Xi's three-day visit, reflecting Beijing's concern that there should be no embarrassment ahead of a key Communist Party congress later this year which is expected to cement his position as the most powerful Chinese leader in a generation.
Xi said Saturday that any threat to China's sovereignty and security or to the power of the central government "crosses the red line and is absolutely impermissible".
He also warned against anyone endangering Hong Kong's constitution or using the city "to carry out infiltration and sabotage activities against the mainland".
The message comes as young activists have emerged calling for self-determination or even full independence for Hong Kong, which has infuriated Beijing.
Just hours after Xi left the city Saturday afternoon, thousands marched from Victoria Park to government offices in central Hong Kong in an annual pro-democracy protest which also called for the release of cancer-stricken Chinese dissident Liu Xiaobo, who was given medical parole earlier this week but remains on the mainland.
"Suppression by the regime has never let up and I don't see any end in sight," said activist Nathan Law, Hong Kong's youngest legislator, who was arrested Wednesday over an anti-China sit-in and attended the protest after being released Friday.
Xi insisted that Hong Kong had "more extensive democratic rights and freedoms than at any other time in its history" in his speech and pledged to uphold its semi-autonomous status.
But Beijing's foreign ministry declared Friday that the document signed by Britain and China which initiated the handover "is no longer relevant".
The Sino-British Joint Declaration gave Hong Kong rights unseen on the mainland through a "one country, two systems" agreement, lasting 50 years.