WASHINGTON, June 23— The Supreme Court, by a vote of 7 to 2, today upheld a Federal law aimed at encouraging states to raise the legal drinking age to 21 years by withholding some highway grants from those that fail to do so.

In a second case, the Justices unanimously overruled an 1861 Supreme Court decision that Federal courts have no power to order state governors to extradite criminal suspects to other states. The Justices held that Iowa must extradite a man charged in Puerto Rico with murdering a pregnant woman there.

The drinking-age decision essentially leaves the status quo intact by removing doubt about the validity of the 1984 Federal law. Twenty-three states, including New York and Connecticut, have raised their drinking ages since it was passed. The ruling was widely expected.

Chief Justice William H. Rehnquist's majority opinion rejected arguments by South Dakota and several state and local government groups that the law violated constitutional principles of federalism and the broad powers of the states under the 21st Amendment to regulate sales of alcoholic beverages. He called the law a ''relatively mild encouragement to the states to enact higher minimum drinking ages than they would otherwise choose.'' #2 Justices Disagree Justices Sandra Day O'Connor and William J. Brennan Jr. dissented separately. Justice O'Connor said the law was ''an attempt to regulate the sale of liquor'' that encroached on the powers of states under the 21st Amendment, which repealed Prohibition and authorized states to regulate sales of alcoholic beverages.

A major goal of the 1984 law was to reduce highway deaths by preventing teen-agers from drinking and driving, and especially to remove the incentives to cross state lines to buy liquor.

It provided for withholding part of Federal highway funds otherwise due to states that allow people under 21 to drink. Five percent of a state's highway funds can be withheld for the current fiscal year, and the figure will rise to 10 percent for the next fiscal year. The total cost for a state that does not have a 21-year-old drinking age could come to many millions of dollars.

As of last March, six states and the Commonwealth of Puerto Rico had legal drinking ages for some or all alcoholic beverages of 18 or 19, according to a General Accounting Office study. Those states are South Dakota, Wyoming, Montana, Louisiana, Colorado and Ohio. Under South Dakota law, 19-and 20-year-olds are permitted to buy low-alcohol beer.

All other states had adopted 21-year-old drinking-age laws. Nineteen of them, including New Jersey, had adopted such laws before 1984, and were essentially unaffected by the new Federal law. Decision Was Expected

Critics of the law said today that they were disappointed but not surprised by the decision today, which followed a long line of Supreme Court rulings that the Federal Government has broad powers to attach conditions to grants of money to state and local governments and others.

Chief Justice Rehnquist said in his opinion that the Court did not have to decide whether the 21st Amendment would bar Congress from legislating directly a national minimum drinking age. He said past decisions had already established that ''the constitutional limitations on Congress when exercising its spending power are less exacting than those on its authority to regulate directly.''

He said in his opinion the 1984 law's purpose of reducing drunken driving was ''directly related to one of the main purposes for which highway funds are expended: safe interstate travel.''

Justice O'Connor, in her dissent, said the vast majority of highway deaths involving drinking were caused by people over the age of 21. ''Establishment of a minimum drinking age of 21 is not sufficiently related to interstate highway construction to justify so conditioning funds appropriated for that purpose,'' she said.

South Dakota had argued in its appeal (South Dakota v. Dole, No. 86-260) that its law fostered ''temperance and safety'' by giving 19- and 20-year-olds access to ''legal drinking, as opposed to surreptitious drinking that will inevitably occur.'' Ruling on Extradition

The extradition decision (Puerto Rico v. Branstad, No. 85-2116) presented a historical paradox.

Justice Thurgood Marshall, the only black Justice in the Court's history, noted that the 1861 decision that the Court was overruling had shielded a ''free man of color'' from being extradited from Ohio to Kentucky to face trial on the eve of the Civil War for ''the crime of assisting the escape of a slave'' in Kentucky.

The Court's ruling that Federal courts may order unwilling governors to extradite fugitives to other states also could have considerable practical significance.

It involved a man whom two Iowa governors in succession had refused to extradite at the request of Puerto Rico after he had jumped bail on the murder charge there.

One of his lawyers had stated at one point in the Iowa proceeding that a white American man ''could not receive a fair trial in the Commonwealth of Puerto Rico,'' Justice Marshall noted in his opinion.