MIAMI — The former president of Panama faced a federal magistrate judge in Miami on Tuesday, in a plea to stay out of jail and fight extradition on charges that he “commandeered” sophisticated wiretapping gear to listen in on political enemies, business rivals and even a mistress.

Ricardo A. Martinelli, 65, who governed Panama from 2009 until 2014, was arrested last week in Coral Gables, Fla., on illegal surveillance and embezzlement charges. Mr. Martinelli moved to South Florida shortly after his presidency ended and requested political asylum, saying the new president had lodged a politically motivated case against him.

Panama wants him back.

While Tuesday’s bail hearing before United States Judge Edwin G. Torres covered topics like flight risk and the complexities of international extradition treaties, it also offered a view of the unorthodox methods that governments use to keep track of their foes.

Prosecutors say Mr. Martinelli diverted $13.4 million meant for social programs for the poor to pay for software that allowed him to hack into cellphones. He is accused of running a stealth national security operation to keep an eye on dozens of business competitors, opposition lawmakers and union activists to glean embarrassing and compromising information on them.