The formal move to end the prosecution of the New York City mayor came after days of turmoil in the Justice Department.