Demonstrators protest against gerrymandering at a rally at the Supreme Court during the gerrymandering cases Lamone v. Benisek and Rucho v. Common Cause.

The Supreme Court ruled Thursday that federal courts may not block gerrymandering in a 5-4 decision that fell along partisan lines.

The court also ruled, in a separate high-profile case decided Thursday, that the Trump administration's reasoning for adding a citizenship question to the 2020 census was insufficient, effectively blocking the question for now.

On the final day of decisions before the court's summer recess, Chief Justice John Roberts delivered the majority opinions of the court in both cases.

The closely watched case on a charged political matter comes in the midst of the 2020 presidential election. The decision was met with scorn by some Democrats running for president, including former vice president Joe Biden, and a sharp dissent from the liberal justices.

"We conclude that partisan gerrymandering claims present political questions beyond the reach of the federal courts," Roberts wrote in the redistricting case. He said those asking the top court to block gerrymandered districts effectively sought "an unprecedented expansion of judicial power."

"Federal judges have no license to reallocate political power between the two major political parties, with no plausible grant of authority in the Constitution, and no legal standards to limit and direct their decisions," he wrote.

The court's decision prompted a fierce reply from its liberal wing. Justice Elena Kagan wrote a dissent joined by Justices Ruth Bader Ginsburg, Stephen Breyer and Sonia Sotomayor.

"Of all times to abandon the Court's duty to declare the law, this was not the one," Kagan wrote. "The practices challenged in these cases imperil our system of government. Part of the Court's role in that system is to defend its foundations. None is more important than free and fair elections."

Kagan wrote that she disagreed with the majority with "respect but deep sadness."

The justices considered two cases, out of North Carolina and Maryland, in which voters alleged that their congressional districts were unfairly drawn to benefit one political party. The top court had never declared a district map as too partisan.

During arguments in March, the conservatives seemed reluctant to weigh in on the matter. At the time, Justices Neil Gorsuch and Brett Kavanaugh, President Donald Trump's two nominees, suggested that it may be better to leave the issue to the states.

That argument was taken up by Roberts in his opinion. Roberts wrote that the court's conclusion did not "condemn complaints about districting to echo into a void," noting that states "are actively addressing the issue on a number of fronts."

Roberts cited state constitutional amendments passed by Colorado and Michigan that created commissions to approve district maps, and a new position created by Missouri voters — "state demographer" — that will draw legislative district lines.

In a post on Twitter shortly after the opinion was released, Biden, who is leading the crowded field of Democrats seeking their party's nomination for president, wrote that the court "refused to stop politicians rigging our democracy by writing election rules for their own benefit."

"It couldn't have happened without Justices put there by Donald Trump and Republicans — another reason why Democrats must take back the White House in 2020," he wrote.

Sen. Bernie Sanders of Vermont wrote in a Twitter post that the "Supreme Court gave Republican politicians across the country the approval to rig our democracy and suppress voters" and said "We can take back power by registering and mobilizing new voters."

Sen. Elizabeth Warren of Massachusetts called the decision an "abomination" and vowed to "push to require states to use independent redistricting commissions to draw fair districts."

The two cases came from North Carolina and Maryland. In North Carolina, Democratic voters alleged that a map drawn by the GOP legislature in 2016 unfairly benefited Republicans.

In Maryland, it was Republicans who challenged the map, saying that one congressional district drawn in 2011 was unfairly tilted in favor of the Democrats.

In both cases, those behind the maps admitted that they were drawn to benefit their party.

The cases are known as Lamone v. Benisek, No. 18-726, and Rucho v. Common Cause, No. 18-422.