SINGAPORE—The U.S. State Department ordered the departure of all nonemergency U.S. Consulate staff and their families from Shanghai as the city’s health authorities announced more than 23,000 new daily infections.

The “ordered departure,” which came just days after the State Department had given the green light for employees to leave voluntarily, “means that we are now mandating that certain employees depart Shanghai rather than making this decision voluntary,” a U.S. Embassy spokesman said in a statement Tuesday.

Shanghai, a coastal megacity in eastern China that is one of the country’s most important business and financial centers, has recorded a surge in Covid-19 cases, prompting authorities to impose citywide lockdowns that have confined the city’s 25 million residents to their homes or placed them in centralized quarantine facilities.

Chinese Foreign Ministry spokesman Zhao Lijian said China was “strongly dissatisfied and firmly opposed the U.S.’s politicization of the evacuation.” Mr. Zhao also asked that the U.S. to stop attacking China’s Covid-prevention policies and using the virus as an excuse to smear the country.

As the lockdown stretches into its third week, many in the city have complained that they are running low on food and patience, turning to social media to express displeasure or call for help.

The roughly 23,000 new daily local infection cases announced by health authorities on Tuesday marked the first day in the past 11 that the case count was lower than the day before, suggesting that the outbreak may have reached a peak, at least for now.

The U.S. State Department said in its statement that it had updated its assessment of its officers’ safety in light of the outbreak conditions, adding that it would review its status every 30 days.

Shanghai consulate staff and their family members will depart on commercial flights, the statement read, adding that Beijing embassy officials would fill the void in assisting with the provision of emergency citizen services in Shanghai.

The pullout of American diplomats from Shanghai comes as Shanghai officials have said they would start experimenting with easing up lockdowns in certain parts of the city if no new Covid cases are found, though a list shows more than 7,000 residential neighborhoods remain in complete lockdown.

“The zero-tolerance approach to COVID-19 by the PRC and Hong Kong governments severely impacts travel and access to public services,” the U.S. State Department said in a separate statement.

Late last week, the U.S. issued a travel advisory urging citizens to reconsider travel to China, citing what it called “arbitrary enforcement of local laws and Covid-19-related restrictions.” American citizens were asked not to travel to Hong Kong, Shanghai and the northeastern Chinese province of Jilin because of pandemic-related curbs, which included the risk of parents and children being separated.

In late January 2020, during China’s first wave of the coronavirus outbreak in the Central Chinese city of Wuhan, the U.S. had also organized an evacuation flight for its diplomats from the city. It was one of the first governments to do so.

Write to Liza Lin at Liza.Lin@wsj.com and Vivian Salama at vivian.salama@wsj.com