WASHINGTON — The Supreme Court shielded public employees from being punished or fired if they testify in court against their superiors, ruling Thursday that the First Amendment protects those who tell the truth and reveal corruption.

Such testimony is “speech as a citizen for First Amendment purposes” and deserves to be protected, the court said in a 9-0 decision.

The ruling revives a lawsuit by a former Alabama community-college official. He testified against an influential state legislator who was drawing a paycheck from the college but doing no work. After his testimony, the college official was fired.

Edward Lane, the former official, sued several top college officials, alleging that he was fired for telling the truth in violation of the First Amendment.

He lost in the lower courts, which dismissed his claim on the grounds that the First Amendment does not protect public employees for exposing internal wrongdoing.

The Supreme Court itself had seemed to say that in a 2006 ruling involving the Los Angeles District Attorney’s Office. That decision limited the free-speech rights of public employees.

In the 2006 case, Garcetti v. Ceballos, the court had said the First Amendment does not protect internal whistle-blowers who speak about matters involving “their official duties.” That case arose when a deputy prosecutor disagreed with his superiors over the use of a search warrant and spoke out about the dispute.

In Thursday’s opinion, the justices retreated somewhat from their earlier ruling. Testifying in court is protected because it is not part of an employee’s “ordinary job duties,” the justices explained.

Justice Sonia Sotomayor said public employees who are called to testify in a corruption probe have an “independent obligation” to tell the truth, which “sets it apart” from speaking out about a workplace dispute.

Denying First Amendment protection “would place public employees who witness corruption in an impossible position, torn between the obligation to testify truthfully and the desire to avoid retaliation and keep their jobs,” she said.

The decision in Lane v. Franks revived the lawsuit and sent it back to a lower court.