BEIJING — China’s leader, Xi Jinping, praised the Chinese gambling hub of Macau on Friday for its patriotism and delivered a veiled rebuke to nearby Hong Kong that Beijing would not allow challenges to its sovereignty or the interference of “external forces.”

Mr. Xi spoke on the 20th anniversary of the return of Macau to Chinese rule, a significant event for Beijing as it wrangles with protests in nearby Hong Kong that have evolved into the most direct challenge to Communist Party rule in decades.

The handover of Macau, the former Portuguese colony, in 1999 took place two years after Britain ceded sovereignty of Hong Kong. In the years since, the two cities have functioned as semiautonomous territories with greater economic and political freedoms — up to a limit, Mr. Xi made clear.

Mr. Xi’s remarks amounted to a warning for Hong Kong, where protests and clashes with the police have convulsed the city since June. Macau, he said, provided “a gorgeous chapter” in the history of the arrangement China calls “one country, two systems” by demonstrating its fealty to the central authority in Beijing.