A high school teacher who was fired earlier this year for showing up for jury duty with heroin in his backpack could get his job back.

A judge found that Damian Esteban's firing was "unduly harsh" and ordered the Department of Education to impose a lesser punishment. City officials blasted the decision on Thursday, saying they would appeal.

"We cannot fathom how a teacher who took 20 bags of heroin into a courthouse is fit to stand in front of a classroom and teach the city's school children," Michael A. Cardozo, the city's top lawyer, said in a statement.

Mayor Michael Bloomberg added that the ruling "shows a callous indifference to the well-being of our students."

There was no immediate response to a message left Thursday with Esteban's attorney.

Esteban's troubles began last year when he was returning to a Manhattan courthouse after taking a break as a juror in a murder trial. While passing through security, courthouse officers discovered heroin packets in a cigarette box.

At the time, the 34-year-old Esteban was a teacher at Williamsburg High School for Architecture and Design, making $64,500 a year.

He was kicked off the jury and charged with misdemeanor drug possession. He agreed to attend a treatment program in exchange for having the charge dropped.

After placing Esteban on administrative duty, school officials sought his dismissal at an arbitration hearing earlier this year. The teacher testified at the hearing that he got hooked on heroin after using it to relieve pain from an ankle injury. He also claimed he had kicked the habit, but forgotten that a backpack he took to court had the drug stashed inside.

In May, an arbitrator ordered Esteban's immediate dismissal.

"If the respondent's testimony is truthful, his handling of heroin was consistently cavalier, careless and irresponsible," the arbitrator wrote. "This recklessness creates an unacceptable and ongoing level of risk for the department and the students for whom it is responsible."

Esteban won a reprieve Monday from Judge Manuel Mendez, who agreed with arguments by his lawyer that there was no evidence that his conduct affected his performance as a teacher. He said suspension without pay was a more appropriate penalty.

"The conduct took place off the school site and no student was involved," the judge wrote. "He has a spotless record as a teacher for five years. ... The penalty of termination is excessive and shocking to the court's sense of fairness."