AUSTIN — In a major victory for state officials, the U.S. Supreme Court on Monday reversed lower court rulings that found a number of the state's congressional and state House districts were discriminatory and needed to be redrawn.

The court ruled 5-4 that state lawmakers did not discriminate in the drawing of eight state House districts and two congressional districts in 2013 and that those districts can stand. The court did fault state House District 90 in Fort Worth, held by Democrat Ramon Romero, and it will have to be redrawn.

Five of the affected state House districts — 90, 93, 103, 104 and 105 — are in the Dallas-Fort Worth area. The congressional districts are 27 in Corpus Christi and 35 in Austin.

A three-judge panel in a U.S. district court in San Antonio had ruled the districts discriminatory in August and ordered them redrawn.

Changes to the District 90 boundaries in the Fort Worth area are likely to affect the shape of surrounding districts.

Still, the court's decision was a major defeat for the plaintiffs, which include civil rights groups, voting rights organizations and lawmakers who had hoped to make inroads for larger representation of minorities, who tend to vote for Democrats.

The ruling was also a blow to voting rights advocates, who say the state has worked to limit the voting strength of minorities over the last decade through discriminatory electoral maps and laws that require identification at the polls. Without courts intervening on their behalf, advocates say, the voting strength of minorities will be diluted for years to come.

State officials celebrated the ruling, which is the latest turn in a legal battle that has lasted seven years.

"The court rightly recognized that the Constitution protects the right of Texans to draw their own legislative districts, and rejected the misguided efforts by unelected federal judges to wrest control of Texas elections from Texas voters," state Attorney General Ken Paxton said in a written statement. "This is a huge win for the Constitution, Texas, and the democratic process. Once again, Texans have the power to govern themselves."

The map's challengers said the decision set voting rights protections back.

"We are clearly disappointed that the court reversed multiple findings by bipartisan judges that would have afforded justice to Latino and African-American voters," said state Rep. Rafael Anchia, a Dallas Democrat and the chairman of the Mexican American Legislative Caucus, a plaintiff in the case. "The rules were changed today. Voting rights protections were set back decades with this decision. ... The Supreme Court's ruling today is an extension of the Trump administration's abuse of power against communities of color."

In 2012, the U.S. district court in San Antonio issued an order recommending changes to districts based on its finding that the state's 2011 congressional and state House maps discriminated against minorities.

The Legislature argued that the changes it made in 2013 could not be found discriminatory because they narrowly followed the court's guidance from 2012.

The Supreme Court's majority opinion, written by Justice Samuel Alito, a conservative stalwart, said the lower court had "committed a fundamental legal error" last year when it ruled the maps discriminatory.

The court's opinion said the three-judge panel's order had placed the burden of proof on the state to prove it had fixed the maps from 2011, rather than on the plaintiffs to show that the lawmakers had acted with ill intent.

"It was the challengers' burden to show that the 2013 Legislature acted with discriminatory intent when it enacted plans that the court itself had produced," Alito wrote. "The 2013 Legislature was not obligated to show that it had 'cured' the unlawful intent that the court attributed to the 2011 Legislature. Thus, the essential pillar of the three-judge court's reasoning was critically flawed."

In changing the 2011 maps, state lawmakers had stuck very closely to the recommendations given by the court and made few other changes. Therefore, the court said, the lawmakers should be given the presumption of good faith.

But in changing House District 90, the state did stray from the San Antonio court's recommendations, so the Supreme Court ruled that it was invalid and must be redrawn. The case is back at the district court level, which will determine when and how House District 90 will need to be redrawn.

During oral arguments in April, the justices debated whether the Supreme Court had jurisdiction in the case. Alito wrote that it did because the lower court's order had the "practical effect" of blocking the map's use in the next election.

Justice Sonia Sotomayor and the liberal wing of the court dissented from the majority opinion.

"The Court today goes out of its way to permit the State of Texas to use maps that the three-judge district court unanimously found were adopted for the purpose of preserving the racial discrimination that tainted its previous maps," Sotomayor wrote in their dissent.

She said the "disregard of precedent and fact comes at a serious cost to our democracy."

"After years of litigation and undeniable proof of intentional discrimination, minority voters in Texas — despite constituting a majority of the population within the State — will continue to be underrepresented in the political process," she wrote. "Those voters must return to the polls in 2018 and 2020 with the knowledge that their ability to exercise meaningfully their right to vote has been burdened by the manipulation of district lines specifically designed to target their communities and minimize their political will.

"The fundamental right to vote is too precious to be disregarded in this manner."

CORRECTION, 12:40 p.m., Jun 25, 2018: An earlier version of this story incorrectly said the Supreme Court's ruling allowed 10 state House districts to remain intact, but the number is eight.