Carrie Lam was sworn in Saturday as Hong Kong’s chief executive, becoming its first female leader and the fourth person to hold the top government post since China regained control of the former British colony in 1997.

She was previously the top deputy to Leung Chun-ying, the unpopular chief executive who chose not to seek a second term.

Mrs. Lam was sworn in by President Xi Jinping of China on his final day of a three-day visit to mark the 20th anniversary of Hong Kong’s return to Chinese control. Mr. Xi said the “one country, two systems” model, under which the city maintains autonomy in local government and its legal, economic and financial systems, was facing “new situations” and “new problems.” He pledged the central government’s continuing support to Hong Kong.

The ceremony, in a large convention center along Victoria Harbor, came hours before thousands of demonstrators planned to gather for an annual march to protest the lack of full democracy in Hong Kong.