On Friday Sheriff Mirkarimi, who has been the target of the most damning criticism over the episode, stood in front of large pack of television cameras and laid the blame for the release of Mr. Lopez-Sanchez on federal officials, saying that his office had done nothing wrong and had followed all legal procedures.

“What we’re seeing is a lot of finger-pointing and distortion,” Sheriff Mirkarimi said. Under an ordinance that the city passed in 2013 — which received unanimous approval from the Board of Supervisors and was quickly signed into law by Mr. Lee — local law enforcement officials are not allowed to keep undocumented immigrants in custody simply for being in the country illegally. He cited that law as the reason Mr. Lopez-Sanchez was released. For Sheriff Mirkarimi, the limelight could have hardly fallen on a man with fewer political allies.

Earlier this year, there were news reports that the sheriff’s deputies had forced prisoners in jail to fight one another, which prompted some of his staunchest supporters to say they had lost faith in him. He is facing a tough battle for re-election this fall, and law enforcement unions back his challenger.

The sheriff said on Friday that the blame directed at him was simply politics.

“The mayor is throwing his own law under the bus,” Sheriff Mirkarimi said. “If, in fact, he is looking for some loophole in the law, it really defeats the whole purpose of the law in the first place.”

When immigrant rights advocates lobbied for the city’s ordinance, called Due Process for All, in 2013, to stop local officials from cooperating with federal immigration agents, Mr. Lee pressed for leeway for law enforcement and eventually reached a compromise with the supervisors. But Sheriff Mirkarimi, who had long opposed working with federal immigration officials, was mostly left out of negotiations because many groups were reluctant to be associated with him, said Bill Hing, a law professor at the University of San Francisco and the founder of the Immigrant Legal Resource Center.