An FBI agent is being held on anti-terrorism charges in Pakistan after authorities found ammunition in a bag as he boarded a plane in Karachi, Pakistani and U.S. officials said Tuesday.

Joel Cox, 32, was detained by airport police in Karachi about 4 p.m. Monday when he tried to board a Pakistan International Airlines flight to Islamabad. He was in possession of 15 bullets and a magazine for a 9mm pistol, police officials said.

On Tuesday, he appeared in court on charges that he had violated local anti-terrorism laws that prohibit the carrying of weapons or ammunition on a commercial flight. A judge ordered that Cox be detained until at least Saturday so Pakistani security officials can investigate the matter.

The American’s arrest was news across Pakistan, and one television station aired footage of Cox sitting in a jail cell in Karachi, the country’s largest city and one of its most dangerous .

U.S. officials in Washington confirmed that the agent, who is assigned to the FBI Miami Field Office, was in Pakistan on temporary duty. They had earlier requested that Cox’s name be withheld, citing the sensitivity of the situation, but his name has now been widely publicized in Pakistan. The FBI did not return calls Wednesday morning.

A U.S. official with knowledge of the case said the agent was not armed and had apparently forgotten about the loaded magazine in his bag. Cox was in Pakistan as part of a multi-agency effort to help the Pakistanis investigate corruption, the official said.

Reached by phone, Cox’s father said his son was scheduled to be in Pakistan for about three months for “office-type work” with “a non-FBI-type” entity.

An FBI spokesman in Miami referred questions about the arrest to the State Department.

Meghan Gregonis, a spokeswoman for the U.S. Embassy in Islamabad, said U.S. officials are working to resolve the matter.

“We are aware of the reports, and we are coordinating closely with Pakistani authorities on the matter,” Gregonis said.

State Department officials also voiced optimism that the matter can be quickly resolved. But a Pakistani Foreign Ministry official, speaking on the condition of anonymity to discuss the sensitive subject, said officials are trying to gather more information about the agent’s job in Pakistan.

It’s common for FBI agents to be assigned overseas, where they often work out of U.S. consulates or embassies.

One former FBI agent who used to work in Pakistan said agents are allowed to carry weapons there. But the former agent, who spoke on the condition of anonymity to be candid, said they are not allowed to carry weapons onto civilian aircraft.

In recent years, several Americans have been detained in Pakistan on charges that fueled diplomatic tension between the two countries. In the most high-profile case, a CIA contractor was detained for nearly two months in 2011 in the killing of two men in Lahore.Raymond A. Davis, who was part of a secret CIA team that had been operating in the eastern city, argued that he had acted in self-defense after the two men tried to rob him.

The incident sparked violent protests across Pakistan and greatly strained bilateral relations. Pakistan initially rebuffed requests from senior Obama administration officials that Davis be granted diplomatic immunity. But he was eventually freed after arrangements were made to compensate the relatives of the victims.

At the time, anti-American sentiment was growing in Pakistan because of U.S. drone strikes and disagreements over whether the Pakistani military was doing enough to combat terrorism.

But in the past year, officials on both sides have stressed that relations are on the upswing.

On Monday, the Pakistani military announced that the U.S. military was being granted rare permission to use Pakistani airspace so it could more safely transport vehicles and other supplies from Karachi to Afghanistan.

Craig reported from Kabul. Julie Tate in Washington, Nisar Mehdi in Karachi and Shaiq Hussain in Islamabad contributed to this report.