HONG KONG — President Xi Jinping of China delivered a tough speech Saturday at the end of a three-day visit to the semiautonomous Chinese city of Hong Kong, warning against politicizing disputes or challenging the authority of the central government. But he didn’t stick around for the reaction, as thousands of people took to the streets in an annual protest calling for greater democracy.

Mr. Xi came to Hong Kong to mark the 20th anniversary of the former British colony’s handover to Chinese control. He inspected thousands of People’s Liberation Army troops stationed here, met with local dignitaries and swore in Carrie Lam as the city’s new chief executive, the top local official.

His praised the city for its success as a prosperous global hub of trade and finance, but he also warned against resistance to Beijing’s control and influence, which has bubbled here for years.

Hong Kong is a “plural society” with “different views and even major differences on some issues,” Mr. Xi said while speaking to dignitaries at the inauguration of Hong Kong’s government at the city’s convention center along Victoria Harbor.