The Supreme Court, overturning a 21-year-old precedent, ruled 7 to 2 yesterday that prosecutors may not exclude blacks from juries on the basis of race, and gave defendants the right to immediately challenge any effort to do so.

Until yesterday's ruling, prosecutors enjoyed complete power to remove a limited number of potential jurors from a trial for any or no reason. Their use of such peremptory, or automatic, challenges could not be questioned.

The court acted after years of complaints that a 1965 ruling, Swain v. Alabama, though condemning discrimination, continued to allow prosecutors to fashion all-white juries for black defendants.

In an unusual action, the author of that ruling, Justice Byron R. White, agreed with the decision to overturn it. "It appears," White said yesterday, "that the practice of . . . eliminating blacks from . . . juries in cases with black defendants remains widespread . . . so much so that I agree that an opportunity [to protest such actions] should be afforded when this occurs.

"I agree with the court that the time has come to rule as it has, and I join its opinion and judgment," he said.

"Purposeful racial discrimination," Justice Lewis F. Powell Jr. wrote for the majority, "violates a defendant's right to equal protection because it denies him the protection that a trial by jury is intended to secure" -- a jury of his peers.

As a result of yesterday's decision in Batson v. Kentucky, a minority defendant may now object if a prosecutor uses automatic or discretionary challenges to remove members of his race from the jury. The prosecutor will then have the burden of convincing the judge that his reasons are not racially motivated.

In addition, Powell said, the prosecutor may not respond that he was using "his intuitive judgment" that the excused potential juror "would be partial to the defendant because of their shared race."

"Just as the Equal Protection Clause forbids the states to exclude black persons from [juries] on the assumption that blacks as a group are unqualified to serve as jurors," Powell said, "so it forbids the states to strike blacks [potential jurors] on the assumption that they will be biased in a particular case simply because the defendant is black.

"The core guarantee of equal protection, ensuring citizens that their state will not discriminate on account of race, would be meaningless were we to approve the exclusion of jurors on the basis of such assumptions."

"Selection procedures that purposefully exclude black persons from juries undermine public confidence in the fairness of our system of justice," Powell said, rejecting arguments by Kentucky and the Reagan administration to uphold the use of prosecutors' powers.

Justice Thurgood Marshall, the court's only black member, joined Powell's "eloquent opinion," saying it takes a "historic step toward eliminating the shameful practice of racial discrimination in the selection of juries."

However, Marshall said the ruling did not go far enough because, once challenged, "any prosecutor can easily assert . . . neutral reasons for striking a juror, and trial courts are ill-equipped to second-guess those reasons."

It may be difficult, Marshall said, referring to recent lower court rulings, for trial judges to deal with "a prosecutor's statement that he struck a juror because the juror had a son about the same age as defendant, or seemed 'uncommunicative,' or 'never cracked a smile.' "

Justice William H. Rehnquist, joined by Chief Justice Warren E. Burger, dissented, saying the court's overruling of Swain was "ill-considered and unjustifiable."

"In my view," Rehnquist said, "there is simply nothing 'unequal' about . . . using peremptory challenges to strike blacks from the jury in cases involving black defendants so long as such challenges are also used to exclude whites in cases involving white defendants, Hispanics in cases involving Hispanic defendants, and Asians in cases involving Asian defendants, and so on."

Rehnquist said that "as long as they are applied across the board to jurors of all races and nationalities, I do not see -- and the court most certainly has not explained -- how their use violates the Equal Protection Clause."

Steve Ralston, an attorney with the NAACP Legal Defense and Educational Fund Inc., which lost the Swain case, said yesterday's ruling will have an important "prophylactic effect" in ending widespread discrimination in jury selection.

Before yesterday's ruling, Ralston said, prosecutors could discriminate "because Swain said they could. Now they have been told this is unconstitutional."

California and Massachusetts, which several years ago adopted the rule set down yesterday, have not had difficulties administering the guidelines, he said.

But Donald Kuebler, a Michigan prosecutor with the National District Attorneys Association Inc., said the ruling may lead to widespread "mini-trials" every time a prosecutor tries to remove a black or other minority from a jury.

In other action, the court ruled 7 to 2 that a defendant facing the death penalty has a right to question prospective jurors about racial bias if the crime involves interracial violence.

The decision overturned the death sentence given to Willie Lloyd Turner, a black, for murdering a white jewelry store owner during a 1978 robbery in Franklin, Va.

Writing for the majority, White said that while defendants facing lesser charges do not have such a right, capital punishment cases are different because, in sentencing, "the jury is called upon to make a highly subjective, unique, individualized judgment regarding the punishment that a particular person deserves."

White said "the risk of racial prejudice infecting a capital sentencing proceeding is especially serious in light of the complete finality of the death sentence."

Justice William J. Brennan, in partial dissent, said the decision, "though clearly half right, is more than clearly half wrong." He said the court should not only overturn the death sentence but should overturn the conviction as well.

Marshall dissented to limiting the ruling to death penalty cases.

Powell and Rehnquist said neither the conviction nor the sentence should be overturned and that the majority "constitutionalizes the unjustifiable assumption that jurors are racially biased."

In a third ruling yesterday, the court ruled unanimously that an antiabortion activist did not have a legal basis to ask the court to uphold an Illinois law restricting abortions.

Justice Harry A. Blackmun said only the state, not an individual, could appear in court to defend a state law. The ruling leaves intact a federal appeals court ruling striking down the law as an unconstitutional infringement on the right to abortion.

Yesterday's action leaves one abortion case, from Pennsylvania, pending before the court.