The trade deal with Mexico happened quickly because of U.S. tariff threats, President Donald Trump told CNBC on Monday.

The president said the deal came together in just two days because Mexico's leaders knew the alternative would have been worse.

"Tariffs are a beautiful thing when you're the piggy bank," Trump said.

"As soon as I put tariffs on the table, it was done. It took two days," the president told CNBC's Joe Kernen during the telephone interview on "Squawk Box."

The interview featured Trump riffing on a number of topics, including the continuing trade negotiations with China, his views on the Federal Reserve and the just-announced Raytheon-United Tech merger.

The tense negotiations with Mexico took place as the U.S. already was locked in a tariff battle with China and had placed duties on a number of other imports. Trump has said tariffs are essential to restore economic fairness between the U.S. and its global trading partners.

In the case of Mexico, though, the administration used tariffs to accomplish the political goal of stemming the illegal immigration flow.

"We talked about it for months and months and months and they wouldn't get there, and we said, 'Hey, look, if you don't get there, we're just going to have to charge you hundreds of billions of dollars in taxes," he said.

"If we didn't have tariffs, we wouldn't have a deal with Mexico," he added. "We got everything we wanted and we're going to be a great partner to Mexico now because now they respect us. They didn't even respect us. They couldn't believe how stupid we were with everything that's going on where somebody comes in from Mexico and just walks right into our country and we're powerless to do anything."

The White House had threatened to slap tariffs in Mexican imports if it didn't pledge to help stop the flow of illegal immigrants across the border. In a deal announced late Friday, Trump said Mexico had agreed to follow through on earlier concessions it had made on immigration.

Financial markets had been nervous about the standoff and moved higher Monday morning following the announcement.

In his CNBC interview, Trump said the negotiations will result in a more secure border.

"So we're going to be essentially using to a large extent the very powerful immigration laws of Mexico, and Mexico wants to do a good job," he said. "They're moving 6,000 soldiers to their southern border [with Guatemala]. You think they agreed to do that before?"