They get 'em coming and going.

As oil traded above $100 a barrel earlier this decade, JPMorgan Chase  (JPM - Get Report) , Citigroup  (C - Get Report)  and other U.S. banks doled out more than $110 billion of loans to finance a drilling boom -- all the while helping their clients borrow additional cash through bond sales. Now that prices have gone bust, the banks are reaping a bounty of fees underwriting stock sales for financially distressed energy producers, which in turn are using the proceeds to pay down their loans.

And here's the kicker: Some of those oil companies are already taking out fresh loans -- often from the same banks -- to pay for new drilling projects.

The episode highlights the role big banks have played as handmaidens in the oil and gas industry's latest boom-and-bust cycle, while demonstrating their ability to profit in downturns as well as upturns. For the energy markets, the recent flurry of fund-raising could delay a shakeout among crude producers, helping them to maintain output and raising the specter of prolonged prices below $45 a barrel.

"Wall Street has an exceptional business model," said Brad Hintz, a former Morgan Stanley treasurer and bank analyst who's now an adjunct finance professor at New York University. "It collects fees from its corporate clients in both fat and lean times."

The benefit to banks' bottom lines showed up this month, when JPMorgan, Bank of America  (BAC - Get Report)   and Citigroup all beat analysts' estimates for second-quarter results. The surprise, according to executives, came at least partly because the lenders didn't need to sock away as much money as expected to cover energy-loan losses.

While oil prices have rallied from as low as $26 in February, a big driver was the producers' newfound ability to raise money in capital markets, following a drought in investor demand earlier in the year, Citigroup CFO John Gerspach told analysts on a July 15 conference call. In some cases, the companies used stock-sale proceeds to pay down loans. In other cases, the added capital simply made it easier for banks to reclassify distressed borrowers as healthy.

Southwestern Energy  (SWN - Get Report) , a natural-gas producer that's lost $6.4 billion in the last six quarters, raised $1.25 billion in June through a stock sale managed by Credit Suisse  (CS - Get Report) , Citigroup, JPMorgan, Bank of America and Wells Fargo  (WFC - Get Report) , among others. Most of the firms were members of lender consortiums that previously had provided Southwestern with as much as $2.75 billion of loans and credit lines.

EXCLUSIVE LOOK INSIDE: Wells Fargo and Citigroup are holdings in Jim Cramer's Action Alerts PLUS charitable trust portfolio. Want to be alerted before he buys or sells the stocks? Learn more now.

The banks also helped underwrite share offerings for Diamondback Energy  (FANG - Get Report) , Callon Petroleum  (CPE - Get Report) , Pioneer Natural Resources (PXD - Get Report) , Parsley Energy  (PE - Get Report) and QEP Resources  (QEP - Get Report) , regulatory filings show.

In all, Wall Street firms have sold more than $24 billion of stock on behalf of oil and gas companies this year, Hintz estimates. Based on a typical commission of at least 3% for a follow-on stock offering, that works out to more than $720 million of fees.

The surge in offerings was made possible partly by the recent rally in stock markets, which in turn was fueled by speculation that the U.S. Federal Reserve and other central banks will delay raising interest rates -- or in some cases cut them -- to maintain economic growth. Many investors are also betting that the worst is over for oil-company stocks.

"Things are freeing up a little bit," Wells Fargo CFO John Shrewsberry told analysts on a July 15 conference call. "There's a lot more access to capital among energy companies today, of all forms," including junk-bond sales.

U.S. stock offerings, which tumbled 41% in the second quarter versus a year earlier, were up an estimated 67% in July, Morgan Stanley estimated.

Wall Street firms' role as both lenders and underwriters dates to the 1999 repeal of the Depression-era Glass-Steagall Act, which barred commercial banks from selling stocks and corporate bonds. One of the chief goals of the original law was to keep banks from "issuing stock to the unsuspecting public to raise money so they could repay their loans," said Rajesh Narayanan, chair of the finance department at Louisiana State University.

While there's no evidence that such conflicts of interest are occurring now, it's indisputable that banks are benefitting from their ability to offer just about any financial service a company might need, Narayanan said. (Both the Democratic and Republican parties have called for reinstating Glass-Steagall, as a way of shrinking the power of big banks and curbing their risk-taking.)

"We've set up financial institutions that are essentially one-stop shopping for corporations," said Samuel Hayes, a professor emeritus of investment banking at Harvard Business School. "If they're not in a position to take on more debt, the banks are ready to sell them equity."

In an e-mail, Wells Fargo spokeswoman Jessica Ong said that the choice of underwriter is "always at the discretion of the customer," noting that the San Francisco-based bank has been serving the energy industry for decades. She declined to discuss specific companies.

The other banks declined to comment, while Southwestern executives didn't respond to requests for comment.  

For an example of how Wall Street's business model works in practice, take the case of Spring, Texas-based Southwestern. Started 87 years ago as a gas utility, the company in recent decades has morphed itself into the third-largest natural-gas producer in the continental U.S., largely through its use of a drilling technique known as hydraulic fracturing, or fracking.

Much of that drilling was financed with borrowings. Southwestern's long-term debt load swelled 47-fold in the decade through 2015. Milestones included a $2 billion credit line in 2013, $5 billion of acquisition financing in 2014 and at least $3.8 billion of bond sales along the way.

Then last year, as oil and gas prices swooned, the company posted a $4.67 billion net loss. In February, Moody's Investors Service slashed Southwestern's credit rating to junk-grade, citing the company's high debt load.

Southwestern warned in an annual report in February that further downgrades could lead to a liquidity squeeze, and that a prolonged period of low natural-gas prices might force the company to refinance debt, issue new shares or dispose of assets. Or all of the above.

"We cannot assure that any such proposed offering, refinancing or sale of assets can be successfully completed or, if completed, that the terms will be favorable to us," the company said.

Enter the bankers.

On June 27, Southwestern announced that it had reached a debt-refinancing accord with lenders led by Bank of America and JPMorgan, including $2.7 billion of loans that won't come due until 2020.

Two days later, the company launched a stock offering that ultimately placed about 99 million shares -- a 26% increase to its base. The purpose was to raise money to repay a portion of the bank loans and buy back $750 million of bonds, according to Southwestern regulatory filings.

Then on a July 22 conference call, executives announced they would use about $500 million from the stock offering to reactivate drilling rigs on key properties, a move that should provide a significant bump to 2017 production, relative to prior expectations.

Even with the dilution from the stock offering, Southwestern's investors have cheered its strengthened financial position: The shares have climbed 77% this year. (They're still off more than two-thirds from a 2014 high.)

Bill Way, Southwestern's CEO since January, took advantage of the conference call to thank his investors -- and bankers.

"We were in an early spot that was difficult, in a terrible commodity price environment, and they supported us as we navigated forward," he said, according to a company transcript.