The 77-year-old loophole that created Puerto Rico's unique market and led to its ultimate meltdown

San Juan, Puerto Rico.

Puerto Rico, as a U.S. territory, has had favorable tax treatment for its investors for decades. The island is a separate fiscal jurisdiction from the mainland U.S., a status that offers its residents unique tax protections.

Income that a resident of Puerto Rico earns from a bond issued by the commonwealth is 100 percent free from U.S. taxation; however, if a resident owns a security issued outside of Puerto Rico, the income is subject to U.S. taxes, just like any other American citizen.

This is what "led ultimately to the creation of a capital market in Puerto Rico," Carlos Ubinas, the current president of UBS Puerto Rico's broker-dealer and trust company, explained to a FINRA panel in 2015 during sworn testimony.

UBS Puerto Rico, a subsidiary of UBS Financial Services headquartered in Weehawken, New Jersey, is the largest broker-dealer on the island, controlling about 49 percent of total retail brokerage assets at its peak.

The broker-dealer has been the primary underwriter of a series of closed-end fund companies since the mid-1990s. And managed or co-managed 23 proprietary closed-end funds that could only be sold to Puerto Rico residents and corporations whose primary place of business is on the island.

Two types of funds, open-end and closed-end mutual funds, are managed by UBS Asset Managers of Puerto Rico. Each fund offers "its own investment strategy, management style and level of risk," the fund disclosure document says.

The open-end funds allow investors to continually buy and redeem shares by adding or withdrawing their money from the fund with no limit to the number of shares that can be issued.

The closed-end funds issue a fixed number of shares to raise capital, similar to how an initial public offering stateside operates. After the IPO, the shares of the funds are traded in the secondary market between investors.

"They were an extremely popular investment on the Island until the 2013 market collapse because they consistently provided high rates of return with very substantial tax advantages that were only available to Puerto Rico residents who invested in Puerto Rico securities," UBS said in a statement, in response to CNBC inquiries about the funds.

The funds provided Puerto Rico-based investors the ability to own a portion, up to 33 percent of the fund's total assets, of U.S. mainland-issued securities, while still retaining the characteristics of a Puerto Rico investment. At least 67 percent of the fund's total assets had to be invested in Puerto Rico obligations in order for the income to be tax-free.

"The [closed-end funds] provided diversification as up to one third of the holdings were in obligations issued or guaranteed by the U.S. government," a UBS spokesman said.

They were marketed to investors with the objective of current income, consistent with the preservation of capital.

Craig McCann, Securities Litigation and Consulting Group, Inc Founder and Principal.

At its peak, UBS had the largest wealth management business in Puerto Rico, representing an estimated 20,000 households. By 2012, UBS investors on the island had about $10 billion invested in the funds, or roughly 10 percent of the island's gross domestic product.

Additionally, the closed-end funds in Puerto Rico, since they are offered in a U.S. territory, rather than a U.S. state, aren't listed on any exchange like a typical U.S. mainland closed-end fund. This is due to an exemption from the Investment Company Act of 1940. That legislation, among other things, requires that funds and the shares they offer to investors, be registered with the Securities and Exchange Commission and limits the use of leverage. It also bars certain types of transactions.

This loophole "allows the funds to engage in some affiliated party transactions that would not be allowed here on the mainland. And it allows them to be more highly leveraged," said Craig McCann, founder and principal of Securities Litigation and Consulting Group (SLCG).

The closed-end funds that UBS Puerto Rico sold were highly leveraged, in many instances roughly 2 to 1. That was the maximum amount allowed by Puerto Rican securities regulations, according to SLCG analysis.

Often clients were also offered lines of credit that could be used for purposes other than buying additional securities — such as buying a house or investing in a business — and were able to use their closed-end fund investments as collateral for the loan. At one point, the credit lines extended (which were overseen by the credit division of UBS Americas in Weehawken) totaled more than $1 billion.

But as UBS points out in its statement, the funds provided very high interest for investors before they imploded.

"Many of the Funds generated tax free returns greater than 12% in 2010, 2011 and 2012," the spokesman wrote in his statement to CNBC.

"Puerto Rico closed-end funds (CEFs) generated more than $3 billion in earnings for investors, in a wide variety of market conditions for two decades," the spokesman said.

'Didn't miss a penny'

At its peak, UBS Puerto Rico's closed-end funds provided a profitable revenue stream for its parent company.

Through the years 2004 to 2008, the direct and indirect revenue generated by the bond funds accounted for 42 to 53 percent of UBS Puerto Rico's total revenue. The profits flowed up the chain to the parent company, UBS AG.

UBS Puerto Rico also provided investment banking services to the commonwealth, typically as part of the underwriting teams.

They advised and underwrote the issuance of the bonds. They were the bond dealer that sold the bonds to the fund. Then UBS underwrote those funds and sold them to their retail clients, said McCann, who is also an expert witness for plaintiffs in FINRA arbitration cases.

"UBS had its finger in every pie," said McCann. "They didn't miss a penny."

McCann, a former senior financial economist at the SEC, believes another major issue with the funds were the "problematic" Puerto Rican bonds that the funds held.

UBS started to replace the bonds in the funds with ones they were the lead underwriter on.

In 2007 and 2008, UBS was the lead underwriter for two securities. One was the island's bonds tied to the Employee Retirement System, which provides pensions for the government's workers. The second were bonds supported by local sales and use tax revenues.

"These bonds ended up being the bulk of what's in these funds, and what caused the losses ultimately," McCann said.

UBS sold 39 percent to 44 percent of the $1.6 billion ERS Series A bonds into its proprietary bond funds, while 98 percent of the $1.1 billion ERS Series B bonds were purchased by UBS and Santander bond funds, according to SLCG analysis. Both sets of securities were exclusively offered in Puerto Rico and generated $22 million in fees for UBS and the co-underwriters.

UBS also was lead (and only) underwriter for the $737 million Cofina 2008 Series A bonds that went to market in August 2008, with UBS selling 99.6 percent of the bonds into its closed-end funds.

This resulted in an immense overconcentration of the two securities within the funds. By the end of June 2013, the ERS and Cofina bonds accounted for more than 90 percent of the net assets in 10 of the UBS funds.

The heavy concentration and high leverage of the funds ultimately led to staggering losses for investors in 2013 after Puerto Rican bonds, including the ERS and Cofina bonds, suffered a massive sell-off.

By the end of 2013, the 23 closed-end bonds funds managed or co-managed by UBS Puerto Rico lost $3 billion in value, or nearly 70 percent, in that year alone, SLCG data show.

Watch: Puerto Rico's broken bonds

The role Wall Street played in wiping out Puerto Ricans' savings