The defense sometimes fails, often succeeds, a result that has gotten quite a lot of criticism, much of it probably deserved. What mostly doesn't get criticized is the fact that actions which are, on their face, obviously criminal — beating someone up is assault and battery, whether or not the perpetrator is a police officer — are being punished, if at all, only by a civil suit.
The reason is quite simple. Legally speaking, the victim of the crime is not the person who was beaten up, it is the state he lives in. If Mr. Smith assaults me and the case comes to trial, it will be not Friedman vs Smith but State of California vs Smith. Criminal prosecution is controlled by the state, so crimes the state does not want to prosecute don't get prosecuted. If Mr. Smith happens to be a police officer, the state knows that prosecuting him, convicting him, and locking him up for a year will make it harder to hire police officers, as well as provoking conflict with the police union. So, most of the time, it doesn't. A civil case is created and controlled by the actual victim, so in practice civil cases are usually the only way of punishing criminal acts by people the state approves of, such as its employees.
This issue was first brought to my attention in a case where the crime in question was not assault and battery but first degree murder, the killing of two Black Panthers by Chicago police back when I was a graduate student in Chicago. None of the killers were tried, but the city, state, and county ended up settling civil claims for well over a million dollars.
There is a possible solution, one that actually existed in a legal system ancestral to ours. In England in the 18th century, any Englishman could prosecute any crime. In one famous case, a magistrate instructed troops to open fire on a crowd of demonstrators, several people were killed, and the magistrate ended up tried for murder.
If he had been convicted the King could have pardoned him, but pardoning an official, or a policeman, who has been convicted of murder is a much more visible act than never charging him. And England in the 18th century still had in law, although not in practice, a legal action, the Appeal of Felony, which was an entirely private suit for a criminal penalty. The King was not a party to the suit and so, according to Blackstone, could not pardon a convicted defendant.
For more on the subject, see the chapter on 18th Century England in my Legal Systems Very Different from Ours. A late draft is webbed.