UBS agreed to buy its struggling rival Credit Suisse for 3 billion Swiss francs ($3.2 billion) on Sunday, with Swiss regulators playing a key role in the deal as governments sought to stem a contagion threatening the global banking system.

“With UBS’s takeover of Credit Suisse, a solution has been found to secure financial stability and protect the Swiss economy in this exceptional situation,” read a statement from the Swiss National Bank, which noted that the central bank was working with the Swiss government and the Swiss Financial Markets Authority to bring about a merger of the country’s two largest banks.

The terms of the deal will see Credit Suisse shareholders receive 1 UBS share for every 22.48 Credit Suisse shares they own.

“This acquisition is attractive to UBS shareholders but, let’s be clear, as far as Credit Suisse is concerned, this is a bailout. We have structured a transaction that will preserve the value remaining in the business while limiting our exposure to the downside.” UBS chairman Colm Kelleher said in a statement.

The combined bank will have $5 trillion in invested assets, according to UBS.

“We are committed to making this deal a great success. There are no options in this,” Kelleher said when asked during the news conference if the bank could back out of the deal. “This is absolutely necessary for the financial structure of Switzerland and … for global finance.”

The Swiss National Bank pledged a loan of up to 100 billion Swiss francs ($108 billion) to support the takeover. The Swiss government also granted a guarantee to absorb losses of up to 9 billion Swiss francs from certain assets above a pre-set threshold “to reduce potential risks for UBS,” a separate government statement said.

“This is a commercial solution and not a bailout,” Karin Keller-Sutter, the Swiss finance minister, told a news conference on Sunday.

The UBS deal was squeezed before markets reopened for trading on Monday after Credit Suisse shares registered their worst weekly decline since the corona pandemic began. The losses came despite a new loan of up to 50 billion Swiss francs ($54 billion) granted from the Swiss central bank last week, in an effort to stem the slide and restore confidence in the bank.

News of the deal was welcomed by Treasury Secretary Janet Yellen and Federal Reserve Chairman Jerome Powell in a statement. “The US banking system’s capital and liquidity positions are strong, and the US financial system is resilient. We have been in close contact with our international counterparts to support their implementation,” they said.

Credit Suisse had already fought one a series of losses and scandalsand in the past two weeks sentiment was shaken again as US banks reeled from the collapse of Silicon Valley Bank and Signature Bank.

US regulators’ backstop for uninsured deposits in the failed banks and the creation of a new funding facility for troubled financial institutions failed to stem the shock and threaten to engulf more banks both in the US and abroad.

Credit Suisse Chairman Axel Lehmann said at the press conference that the financial instability caused by the collapse of US regional banks hit the bank at the wrong time.

Despite regulatory involvement in the merger, the deal gives UBS the autonomy to run the acquired assets as it sees fit, which could involve significant cutbacks, sources told CNBC’s David Faber.

Credit Suisse’s scale and potential impact on the global economy is far greater than US regional banks, which pressed Swiss regulators to find a way to bring the country’s two biggest financial institutions together. Credit Suisse’s balance sheet is about twice the size of Lehman Brothers when it collapsed, about 530 billion Swiss francs by the end of 2022. It is also much more globally connected, with several international subsidiaries — making orderly management of Credit Suisse’s situation even more important.

Bringing the two rivals together was not without its struggles, but the pressure to avert a systemic crisis won out in the end. UBS initially offered to buy Credit Suisse for about $1 billion on Sunday, according to multiple media reports. Credit Suisse reportedly backed out of the offer, claiming it was too low and would harm shareholders and employees, This is what people with knowledge of the matter say to Bloomberg.

As of Sunday afternoon, UBS was in talks to buy the bank for “substantially” more than 1 billion Swiss francs, sources told CNBC’s Faber. He said the price of the deal increased during today’s negotiations.

Credit Suisse lost about 38% of its deposits in the fourth quarter of 2022 and disclosed in its delayed annual report at the beginning of last week that outflows have not yet reversed. It reported a full-year net loss of 7.3 billion Swiss francs for 2022 and expects a further “relevant” loss in 2023.

The bank had earlier announced a massive strategic review in a bid to tackle these chronic problems, with current CEO and Credit Suisse veteran Ulrich Koerner will take over in July.

— CNBC’s Elliot Smith contributed to this report.