First there were Vallejo, Jefferson County, San Bernadino and Stockton. Then Detroit filed for bankruptcy last summer, leaving retail investors worried that the once-steady municipal bond market was in deep trouble.

Individual investors marched out. Hedge funds, attracted by the bonds’ tax-free earnings and ­attractive rates in its most distressed corners, moved in.

Matters only seemed to grow more dire when the credit rating on the Commonwealth of Puerto Rico’s widely held bonds was downgraded to junk status last month.

As a territory, Puerto Rico is unable to file for bankruptcy — unlike Detroit, where officials are attempting to force bondholders to take an 80 percent haircut in bankruptcy court. But analysts said Puerto Rico’s downgrade meant the cash-strapped island would be charged exorbitant rates if it could borrow money at all, making the threat of default all the more real.

Then Puerto Rico went to the bond market on Tuesday and found a pleasant surprise: Not only were there willing buyers, but they bought bonds yielding just 8.73 percent. Some analysts were expecting investors to demand a 10 percent return for the bonds. The rate is still double what most municipal bonds sell for, but better than many were expecting.

The island sold $3.5 billion of general-obligation bonds. The commonwealth plans to use the funds to balance its budget and refinance expiring debt. Officials have said it should give it enough cash to last until mid-2015. The tax-exempt bonds mature in July 2035.

The sale came during a slow period in the bond market, as local governments borrow less for public improvements and get their budgets in order.

Analysts said the sale shows that the bond market remains attractive — and safe for investors. Despite headline-grabbing bankruptcies, the number of overall defaults — mostly for single projects such as nursing homes, or hospitals — is down in recent years.

“There really haven’t been many defaults, just roughly .3 percent for the market as a whole,” said Matt Fabian, managing director of Municipal Market Advisors, a market research firm. “The fears of widespread defaults are misplaced.”

That said, the analysts said, the island remains some $70 billion in debt and more borrowing has given it more time, not solved its problems.

The debt has hamstrung the island, which has been recession for nearly eight years, crimping tax revenue and pushing the jobless rate well into double digits. The implications of the island’s problems are serious for Americans outside Puerto Rico both because a taxpayer bailout would be expensive and a default would be far more disruptive than Detroit’s record bankruptcy. Officials in San Juan and Washington are adamant that a federal bailout is not on the table, but the situation is being closely monitored by the White House. Meanwhile, the island’s problems have ignited an exodus not seen here since the 1950s, when 500,000 people left for jobs on the mainland.

Fabian and others said moves by island officials to slash pensions, trim budget deficits and pump up tax revenue helped Puerto Rico’s strong performance in the bond markets.

“In part, Puerto Rico, if nothing else, has been willing to do what it can to fix its financial balances and repay bondholders,” Fabian said. “Partly, this is also a statement that price heals all wounds.”

Fabian noted that the 8.73 percent return on Puerto Rican debt is free of state and federal taxes and the equivalent of about a 17 percent return on taxable investments for a wealthy New Yorker.

James E. Spiotto, managing director of Chapman Strategic Advisors, agreed that Puerto Rican officials are working hard to keep the faith of investors.

“Obviously, Puerto Rico is trying to convince the market that it is taking the steps needed to address the issues both from a budget and revenue standpoint,” he said. “They are saying that they have a recovery plan that will work and the market is giving them the benefit of the doubt on that."