NEW YORK (Fortune) -- The Fed's agreement to buy up to $30 billion in troubled Bear Stearns mortgage bonds may have saved JPMorgan Chase from a big writedown, according to senior executives involved in the transaction. Ultimately, it enabled a deal to be done even as alternatives rapidly dried up.

JPMorgan (JPM, Fortune 500) executives initially decided to pass on a purchase of Bear Stearns this past weekend, Bear execs said, largely because of the risks tied to Bear's mortgage portfolios. They changed their minds after the Fed agreed to pony up $30 billion in so-called nonrecourse loans - agreements that transfer the risk of Bear's bad mortgage bets to U.S. taxpayers. The Fed's decision paved the way for the Sunday evening deal that put Bear in JPMorgan's hands for $2 a share, a 93% discount to Friday's closing price.

But the value of Bear's balance sheet wasn't the only worry at JPMorgan. Bear execs say JPMorgan was also worried that without help from the Fed, buying mortgages from Bear (BSC, Fortune 500) could force JPMorgan to write down the value of its own mortgage holdings.

That fear stemmed in part from the sharp decline in the value of mortgage debt this year, along with the different calendars the firms report on. At the end of February, Bear had $16 billion in commercial mortgage-backed securities, $15 billion in prime and Alt-A mortgage bonds and $2 billion in various subprime bonds, JPMorgan said. The value of those securities has been in sharp decline, along with U.S. house prices. Indeed, values in the mortgage securities market have plunged just over the past month, as investors in lenders such as Thornburg Mortgage (TMA) - which is dealing with unmet margin calls triggered by plunging prices - will surely tell you.

JPMorgan's fiscal year ends Dec. 31, so the firm's mortgage holdings are marked to prices that prevailed then - before the bloodbath of the last month. But Bear's latest quarter ended just last month - and Bear executives told Fortune that in that quarter, they marked their mortgage desk's portfolio down by $2.5 billion.

As a result, Bear Stearns' mortgage-portfolio marks could have been lower than JPMorgan's - and bringing the Bear Stearns loans onto JPMorgan's balance sheet could have resulted in a so-called marking event that would have forced JPMorgan to apply Bear's freshly lowered values to its own mortgage book. That could have led to billions of dollars worth of writedowns at JPMorgan - even assuming JPMorgan's marks were entirely appropriate as of Dec. 31.

Midway through the weekend, it had become clear that Bear Stearns had two choices: a deal with JPMorgan or a bankruptcy filing. However, the three-day marathon started out with a glimmer of hope for Bear's beleaguered management. They had access to the Federal Reserve, so an immediate collapse wasn't in the offing (without the Fed's support, the firm would have collapsed late Thursday or early Friday morning) and there were at least "indications of interest" from a handful of prospective suitors.

Banks like HSBC, Royal Bank of Scotland, and J.P. Morgan Chase, as well as financial private equity powerhouse J.C. Flowers, were all said to be interested.

A Bear senior executive told Fortune that the mood inside 383 Madison Avenue was: "The more suitors the better Get some interest going, maybe an auction would occur."

An auction was precisely what did not occur however. HSBC and RBS did not even attend meetings at Bear's headquarters over the weekend; bankers from J.C. Flowers, despite putting in an appearance at headquarters, did not express an interest in putting in a bid, according to Bear executives.

It was understandable why HSBC did not show up, as they largely kicked off the mortgage-market collapse with a stunning $10.5 billion charge in February 2007, capping off with news of another planned $17 billion hit. Interestingly, HSBC had made a series of overtures to Bear going back to 2003, but chief executive Jimmy Cayne had refused to discuss a sale at anything other a multiple to its book value.

RBS' unit RBS Greenwich Capital was a smaller, mirror-image of Bear Stearns, almost entirely focused on fixed-income with a specialty in underwriting mortgage and asset-backed debt. Despite building an impressive new trading floor and headquarters in Stamford, across from UBS, RBS Greenwich Capital is wrestling with its own layoffs and billion-dollar inventory write-downs.

Lastly, despite an expertise in the finance sector, J.C. Flowers didn't have anything like the resources needed to manage a firm like Bear, which had over $102 billion in repo lines outstanding as of last quarter.

JPMorgan, for its part, has so far been one of the best managers of mortgage-related risk on Wall Street. The firm has taken just $3.7 billion in mortgage-related writedowns - a fraction of the hits endured by rivals Citi (C, Fortune 500), Merrill Lynch (MER, Fortune 500) and UBS (UBS). So potentially leaving the Fed with $20 billion worth of troubled securities from Bear's balance sheet - as JPMorgan said Sunday night it intends to do - marks another instance of strong risk management by the bank. A J.P. Morgan official counseled Monday that no decisions have been made yet.

Another oddity in this deal centers on Bear's mortgage trading desk. While much has been justifiably made of that desk's central role during the wide-ranging global credit collapse - it was perennially the first- or second-biggest underwriter on Wall Street - the embattled desk actually made money in the first quarter, Bear execs said.

Over the previous three months, Bear's mortgage and asset-backed trading desks shifted a portion of their capital into a series of proprietary trades designed to profit from the ongoing maelstrom.

Notionally called hedges, in reality they were a series of free-standing bets that Bear executives dubbed The Chaos Trade, given the current climate.

The trades were bets pretty far afield from the standard trading of standard Freddie Mac and Fannie Mae guaranteed securities. The trades speculated on the decline of value in specific tranches of mortgage securities, futures on bond indexes, the shape of the yield curve and individual brokerage and bank bonds.

The payoff for Bear from the so-called chaos trade was impressive: $1.9 billion in one quarter, nearly off-setting the $2.5 billion write-down, Bear execs said. On top of that, one senior Bear executive told Fortune that in the quarter, the firm made $800 million on a series of other mortgage trades, giving its desk a profit for the quarter. The senior Bear executive declined to characterize the nature of the trades.