RALEIGH, N.C. — A federal judge approved a legal settlement Tuesday affirming transgender people's right to use restrooms matching their gender identity in many North Carolina public buildings.

The consent decree between the state's Democratic governor and transgender plaintiffs covers numerous state-owned buildings including facilities run by executive branch agencies that oversee the environment, transportation and Medicaid, among others. In return, the plaintiffs have agreed to drop pending legal action against the governor and other defendants.

The agreement was signed by Judge Thomas Schroeder after a three-year legal battle challenging North Carolina's so-called bathroom bill and the law that replaced it.

"The importance of this cannot be understated — it is about nothing less than the ability to enter public spaces as an equal member of society," said Lambda Legal lawyer Tara Borelli, who represents the plaintiffs. "Nationally, this decree sends an important signal that targeting transgender people for discrimination is unacceptable."

The agreement between the plaintiffs and Gov. Roy Cooper says nothing in the current state law can be interpreted to "prevent transgender people from lawfully using public facilities in accordance with their gender identity" in buildings controlled by the state's executive branch.

The agreement further says executive branch officials, such as the current and future governors, as well their employees at state agencies, are forbidden from using the current law "to bar, prohibit, block, deter, or impede any transgender individuals from using public facilities ... in accordance with the transgender individual's gender identity."

North Carolina's Republican legislative leaders, who passed the "bathroom bill" and its replacement, had opposed the consent decree.

The 2016 law, also known as H.B.2, required transgender people to use restrooms matching their birth certificates in state government buildings and other publicly owned structures including highway rest stops, schools and universities. While that requirement was later rescinded, a replacement law halts new local antidiscrimination ordinances until 2020.

Transgender plaintiffs who had challenged the original law amended their lawsuit to fight the replacement law, arguing that it continued to harm them by creating uncertainty over bathroom rules. They also challenged the moratorium on new local laws to protect LGBTQ people.

Joaquin Carcano, the lead plaintiff and a transgender University of North Carolina employee, hailed the judge's decision in a statement.

"After so many years of managing the anxiety of H.B.2 and fighting so hard, I am relieved that we finally have a court order to protect transgender people from being punished under these laws," he said.

Still, he said the current law's moratorium on new local anti-discrimination laws "remains devastating."

Schroeder had ruled in late 2018 that the replacement law couldn't be interpreted as preventing transgender people from using restrooms in line with their gender identity. Plaintiffs incorporated similar language into their consent decree. The mixed ruling last year, however, rejected some of the transgender people's arguments while letting other parts of the case proceed.

The consent decree was first proposed in late 2017 by the plaintiffs and Cooper, who had inherited a role as a defendant in the case from his predecessor, Republican Pat McCrory. McCrory had signed H.B.2 into law.

Saying the state is "welcoming to all people," Cooper spokesman Ford Porter said in an email Tuesday that: "Today's decision is an important step to putting the harmful impacts of H.B.2 in the rear view mirror for good."

Republican House Speaker Tim Moore and Senate leader Phil Berger, who intervened in the case as defendants, had urged the federal court to reject the consent decree. Their lawyers argued that the plaintiffs were using the latest version of the consent decree to essentially resurrect arguments already rejected by the court.

They also argued the agreement overstepped the proper role of the court because it "purports to bind North Carolina State officers and agencies, in perpetuity, to a temporary political settlement."

Addressing the legislative leaders' concerns in a written order Tuesday, Schroeder noted that nothing in the agreement limits the legislature's ability amend the replacement law "or pass any law it wishes."

Moore spokesman Joseph Kyzer said House lawmakers were reviewing the ruling and assessing options.

Bill D'Elia, a Berger spokesman, said the consent decree essentially confirms executive branch antidiscrimination policies instituted under a 2017 Cooper executive order, while also "putting this lawsuit to bed."

"Hopefully we can finally put this years-old issue behind us and move forward," he said in an email.

FOLLOW NBC OUT ON TWITTER, FACEBOOK & INSTAGRAM