MEXICO CITY — Mexico’s attorney general, a close ally of President Enrique Peña Nieto, resigned on Monday, handing victory to a broad coalition of social groups that have demanded an autonomous prosecutor.

From the moment he was appointed a year ago, Attorney General Raúl Cervantes was a lightning rod.

He leaves office as Mexico’s homicide rate has climbed to its highest level since officials began keeping comparable records 20 years ago. Seventeen former state governors are under investigation on corruption charges, but only three cases have gone to trial.

Many Mexicans have lost faith in their police and prosecutors; a government survey shows that about nine of 10 crimes go unreported. Even the highest-profile cases, like the disappearance of 43 teachers college students in southern Mexico three years ago, seem to defy resolution.

“If there is impunity, if there is violence, it’s because it isn’t being investigated,” said Ana Lorena Delgadillo, executive director of the Foundation for Justice and Democratic Rule of Law in Mexico City.