The San Francisco police officers who shot and killed Mario Woods acted lawfully to protect themselves and bystanders from a man who was armed with a knife, refused to obey commands and tried to flee while under the influence of methamphetamine and other drugs, the city attorney’s office said.

City lawyers laid out a defense of the officers in a response to a federal civil rights lawsuit filed by Woods’ relatives, who say video footage of the encounter shows officers firing at the 26-year-old man from all sides, even though he was shuffling slowly along a wall and did not directly threaten the police.

In their response, city lawyers claimed for the first time that the shooting was lawful and justified.

The Dec. 2 shooting in the Bayview neighborhood, which is under investigation, set off a fierce debate in the city and prompted Mayor Ed Lee to ask for a review of the San Francisco police force by a U.S. Department of Justice division that seeks to heal police-community relations. That review is now under way.

Police officials have said five officers who shot Woods — Winson Seto, Antonio Santos, Charles August, Nicholas Cuevas and Scott Phillips — did their best in a situation where a stabbing suspect still had a knife, wouldn’t heed commands and wasn’t subdued by pepper spray and beanbag rounds. The video of the shooting showed one officer stepping in front of Woods, with the gunfire coming moments later from officers who had surrounded him.

“Failing to surrender peacefully, Woods posed an imminent threat to bystanders and officers,” wrote Deputy City Attorney Sean Connolly in the lawsuit response, which was filed Thursday with the U.S. District Court in San Francisco. “Woods, while still armed with a weapon, attempted to flee by walking past the officers and toward the area where numerous bystanders congregated.”

The city attorney’s office said police officers “need not use the least intrusive form of force when dealing with an armed suspect who poses a threat to the public or police officers. Police officers need not wait until they or a member of the public are assaulted before using lethal force.”

The city filing said Woods told the first officers to arrive that “they would have to shoot him before he would drop the knife.”

An autopsy on Woods, released Thursday by the city medical examiner, found Woods died after suffering 20 bullet wounds, many from behind, and one additional “probable gunshot graze wound” to his right cheek. Investigators found at least 27 bullet-shell casings at the scene. Woods also had four clear bruises from beanbag rounds.

A blood toxicology exam found that at the time of his death, Woods had methamphetamine, marijuana, cough medicine, antidepressants, caffeine and nicotine in his system.

Attorney John Burris, who filed the civil rights lawsuit against San Francisco police, said the number of gunshot wounds “confirms my view that the shooting was excessive. Many and most were in the back.” He said the autopsy report showed that “the officers should have taken the time when they saw he was not responding, and not created a confrontation with him.”

Burris told The Chronicle Friday that he wasn’t surprised by the city attorney’s response.

“I guess what he’s saying is 20 shots in the back were lawful,” Burris said. “That doesn’t make it true.”

Vivian Ho and Demian Bulwa are San Francisco Chronicle staff writers. E-mail: vho@sfchronicle.com, dbulwa@sfchronicle.com Twitter: @VivianHo, @demianbulwa