(Fortune Magazine) -- Bob Riley wanted to help. It was Sunday, Oct. 5, and the Alabama governor was on the phone with Neel Kashkari, a Treasury Department official who the next day would be named by Treasury Secretary Hank Paulson as interim leader of the government's just-approved $700 billion Troubled Asset Relief Program. But Riley couldn't wait for Kashkari's role to become official. He needed to impress upon the new bailout boss the seriousness of the exploding financial crisis in Jefferson County, home to Birmingham. Riley argued that it was urgent that the federal government come to the aid of his state - now.

As he would describe it in a follow-up letter to Kashkari, the situation in Jefferson County was "the single biggest threat to the municipal bond market today and a poster child for how the subprime mortgage crisis is hurting Main Street America."

For months now, Riley and other civic leaders in Alabama have been battling to avert what appears almost certain - that Jefferson County will file for Chapter 9 protection, in what would be the largest municipal bankruptcy in our nation's history. The county has fallen hopelessly behind on payments to service the $3.2 billion it borrowed - on reckless terms - from Wall Street over the past decade to build a new sewer system. As Fortune went to press, the Jefferson County Commission was days away from a vote that could make the bankruptcy official.

Simply put, municipalities aren't supposed to go bankrupt - and rarely do, at least compared with businesses. According to a study by the law firm Mintz Levin, since the bankruptcy laws were written in 1934, there have been fewer than 600 filings for Chapter 9, which provides for the reorganization of municipalities. That's about how many private sector Chapter 11 filings occur, on average, every two weeks. Local governments using stable tax income to pay off money borrowed at a fixed rate may not be sexy, but over history this arrangement has tended to be a pretty reliable bet.

Every decade or so, something big and scary does happen in the normally staid world of public finance: There was a near miss in the '70s when New York City almost went broke ("Ford to city: drop dead" was the famous headline in the Daily News); in the '80s the Washington Public Power Supply System (or WPPSS; the traders called it "Whoops") defaulted on $2.25 billion in loans when it stopped construction of two nuclear power plants; and California's Orange County went into Chapter 9 in the '90s, after the county treasurer made bad bets on interest rates and lost $1.6 billion.

But the saga of Jefferson County stands apart. Unlike previous municipal meltdowns, it is a financial disaster that was to a large degree invented, packaged, and sold by Wall Street. And there are striking parallels to the wider credit crisis that has enveloped the financial world - with overeager borrowers, willing enablers, and dangerously complex financial instruments.

'Magical stuff'

Why punish yourself, local officials were asked, by paying off high-fixed-rate loans when you can lower your payment with auction-rate and variable-rate securities and hedge your bets with swaps? "Magical stuff," is how David Bronner, CEO of the $35 billion Alabama state pension fund, describes the financing, "You're telling everybody, 'Look how much money you saved,' and you forget the risk side of the equation."

The list of potential losers in the county's bankruptcy includes some of the biggest names in banking - past and present. The county's lead underwriter, which brought to market $2.2 billion in debt, is JPMorgan Chase (JPM, Fortune 500), which, ironically, has been getting a lot of good press lately - including in Fortune - for its survival skills. It's less surprising, perhaps, that two of the county's biggest counterparties in derivatives trades were Bear Stearns and Lehman Brothers.

While Governor Riley scrambles to negotiate a settlement, local officials around the country are watching anxiously to see what will happen if the county does enter Chapter 9. Authorities in Connecticut, Florida, and Nebraska, among other states, have recently cut or delayed new financing. And Vallejo, Calif., defaulted on over $200 million back in May. Jefferson County's bankruptcy would only increase the pressure on an already tight muni market.

A soap opera

In Birmingham, though, the macro issues take a back seat to the local drama. Since April, when the county's credit rating was cut to D, the lowest level, and the crisis escalated, the feuding among the state's economic brain trust has provided a daily soap opera for the papers. That Jefferson County got taken by Wall Street is clear. But as if that wasn't bad enough, there has been plenty of waste and corruption over the years too. There have been 22 indictments, 21 convictions, and one guilty plea so far in an ongoing investigation by federal authorities, and the list keeps growing.

Everyone knows that the ultimate target is Larry Langford, the controversial but as yet unindicted mayor of Birmingham. Langford, who served as president of the Jefferson County Commission when most of the riskiest refinancing was completed, has been accused by the Securities and Exchange Commission of accepting more than $156,000 in cash and benefits from brokers who had business with the county.

To have the city's mayor end up in jail would be embarrassing, sure. But it would be nothing compared to the lasting damage to job creation and economic growth that Birmingham's civic leaders - still fighting the legacy of the city's civil rights battles in the 1960s - fear from a Chapter 9 bankruptcy. Yet despite Governor Riley's efforts, there is a feeling of inevitability about what will happen. "The stench of this is starting to permeate across the whole state," says Bronner. More and more, people just want a resolution.

In the beginning, the people of Jefferson County weren't asking for anything special, just a working sewer system - one that didn't overflow whenever it rained, spilling raw sewage into the creeks that drain the Jones Valley basin where Birmingham lies. Lawsuits by environmentalists forced the issue, and in December 1996, Jefferson County signed a consent decree, agreeing to clean things up within ten years. Early estimates of how much it might cost ranged all the way from $250 million to $1.2 billion. In other words, no one had a clue.

As it happened, even the highest estimates were wildly low. Year by year the project expanded and the budget ballooned. Consider the case of the multimillion-dollar tunneling machine. In the late 1990s, Jefferson County began drilling a sewer tunnel beneath the scenic Cahaba River. But a citizen uproar ensued, and the tunnel was halted halfway through. In the end, county officials had to pay nearly $20 million to extract the machine from the hole and return it to the contractors.