BEIJING — China’s top leader backed Chief Executive Carrie Lam of Hong Kong on Monday despite the monthslong protests that have rocked the city and the recent landslide defeat in local elections of political parties aligned with her.

Xi Jinping, China’s president, praised Mrs. Lam for her “courage and responsibility” during what he called the “gravest and most complicated year” in Hong Kong since it returned to China from Britain in 1997.

In an implicit warning to the protesters who seek greater autonomy for the region, Mr. Xi also stressed that Beijing had an “unwavering determination to safeguard national sovereignty, security, and development interests.”

Mrs. Lam’s visit to Beijing over the weekend and on Monday was an annual year-end event for Hong Kong’s leaders, at which their performance is assessed by China’s leaders. But her visit is being closely watched for signs of her fate as Hong Kong’s leader after six months of protests punctuated by frequent street violence.