LOS ANGELES (MarketWatch) — The sweeping health-care law upheld Thursday by the Supreme Court proved to be a boon to hospital stocks and some Medicaid insurers, but commercial insurers faced selling pressure.

The decision came as a surprise, as conventional wisdom said the court would strike down the purchase requirement known as the individual mandate. It apparently surprised investors in large, commercial insurers as well, though the selloff tempered as the day wore on.Read a summary of the Supreme Court’s decision.

Shares of WellPoint Inc. WLP dropped more than 7% at one point and were down 5% at the close. Aetna Inc. AET fell nearly 3%, as did Cigna Corp. CI, +2.15% HealthNet Inc. HNT shed 2%.

“We expect selling pressure on managed care given our belief that investors would have seen an overturn of reform as a better outcome for the sector,” Goldman Sachs analyst Matthew Borsch wrote in a note to clients.

“However, we think any selling pressure will be modest since this ruling is the outcome most investors had anticipated prior to the oral arguments in late March,” Borsch went on to say. “Moreover, this ruling implies less uncertainty than any other outcome, in our view, and avoids the ‘mandate only’ outcome we believe many investors saw as ‘worst case.’ ”

The court also ruled that the federal government cannot withhold funds from states that don’t comply with the law’s goal of expanding Medicaid coverage. States will be allowed to expand coverage but it won’t be required, under the ruling. Read our full story on the court ruling.

That helped shares of Medicaid-focused insurance companies post strong gains, with Wellcare Health Plans Inc. WCG, +1.35% trading up nearly 9%, Molina Healthcare Inc. MOH, +2.94% making similar gains. Amerigroup Corp. AGP, +1.94% adding nearly 5%.

Hospital shares soared with some making double-digit gains. Industry leader HCA Holdings Inc. HCA, +3.61% rallied nearly 11%, while Community Health Systems Inc. CYH, +3.26% gained by 8%. Health Management Associates Inc. HMA jumped nearly 9% and Tenet Healthcare Corp. THC, +5.79% rose more than 5% .

“We see the promise of more than 30 million new patients gaining insurance in [2014] remaining intact, boosting hospital earnings through reduction of uncompensated care expense, which we estimate has been more than 20% over the past few years,” S&P Capital analyst Steven Silver said in a note to clients.

Medical device makers were largely down on the news, but their losses weren’t as great as those sustained by the big insurers. Had the court struck down the entire law, it would have wiped out an impending tax on the device makers. The chances of that happening, however, were considered remote.

The biggest percentage loser was Boston Scientific Corp. BSX, +0.92% , shares of which sold off to the tune of 1.6%, but that represented only a loss of 9 cents. Industry leader Medtronic Inc. MDT, +1.46% was off marginally at the close.

Drug stocks, meanwhile, appeared to follow the broader market downward. The NYSE Arca Pharmaceutical Index DRG, -3.54% inched up to 341. The NYSE Arca Biotechnology Index BTK, +6.20% dipped 2.8% to 1,433.

Of the major pharmaceutical makers, U.K. giant GlaxoSmithKline PLC GSK, +1.20% lost 1.2%, but Pfizer Inc. PFE, +0.78% was off marginally

Weighted heavily by the large insurers that fell Thursday, the health-care sector posted losses, tracking the broader market lower. The Health Care Select Sector SPDR Fund XLV, +1.18% fell less than half a percent, as did the iShares Dow Jones Healthcare Sector Index Fund IYH, +1.13% and the Vanguard Health Care ETF VHT, +1.23% .

Health-care stocks in the S&P 500 index SPX, +1.12% have gained around 6.6% so far this year.

Earlier, trading in some health-care stocks had been briefly halted as news of the court’s ruling broke. They included Tenet, Community Health Systems, WellPoint and Health Management Associates.