A federal appeals court has overturned a ruling ordering Massachusetts prison officials to provide taxpayer-funded sex-reassignment surgery for an inmate convicted of murder.

Michelle Kosilek, born Robert Kosilek, has waged a protracted legal battle for the surgery she says is needed to relieve the mental anguish caused by gender-identity disorder.

In 2012, a federal judge ruled that prison officials must provide the surgery. That decision was upheld by a three-judge panel of the 1st US circuit court of appeals, but prison officials appealed and won a rehearing before the full appeals court.

On Tuesday, the full first circuit found that Kosilek failed to demonstrate that prison officials violated the eight amendment prohibition against cruel and unusual punishment by not providing the surgery.

Kosilek, 65, is serving a life sentence for killing spouse Cheryl Kosilek in 1990.

In his ruling, US district judge Mark Wolf became the first judge in the country to order sex-reassignment surgery as a remedy for an inmate’s gender-identity disorder. Courts around the country have found that prisons must evaluate transgender inmates to determine their health care needs, but most have ordered hormone treatments and psychotherapy.

The state department of correction argued that Kosilek had received adequate treatment for gender-identity disorder, including female hormones, laser hair removal and psychotherapy.

Robert Kosilek is led to county jail following his arraignment on drunken driving charges in New Rochelle, New York, in 1990. Photograph: Frankie Ziths/AP

Prison officials also cited security concerns, saying they were worried about protecting Kosilek from sexual assaults if she completes her gender transition. She is currently housed in a male prison but hopes to be transferred to a female prison after the surgery.

“After carefully considering the community standard of medical care, the adequacy of the provided treatment, and the valid security concerns articulated by the DOC, we conclude that the district court erred and that the care provided to Kosilek by the DOC does not violate the eighth qmendment,” Judge Juan Torruella wrote in the ruling.

The case sparked a heated debate in Massachusetts, where Kosilek’s lawsuit became fodder for radio talkshows and state lawmakers who say the state should not be forced to pay for a murderer’s sex-reassignment surgery.

In his ruling ordering the surgery, US district court judge Mark Wolf found that it is the “only adequate treatment” for Kosilek.

In a 2011 phone interview with Associated Press, Kosilek said the surgery is a medical necessity.

“Everybody has the right to have their health care needs met, whether they are in prison or out on the streets”, Kosilek said.

“People in the prisons who have bad hearts, hips or knees have surgery to repair those things. My medical needs are no less important or more important than the person in the cell next to me.”