The pontiff is due to arrive on Thursday for a nearly four-day visit to celebrate World Youth Day, and a protest march is scheduled for Wednesday evening.
A police official said the suspect arrested in Madrid is a 24-year-old Mexican student specializing in organic chemistry.
She would not say whether investigators believe the man was actually capable of carrying out a gas attack, and did not know if the man actually had chemicals for an attack.
The Mexican embassy identified the detainee as Jose Perez Bautista and said he was from Puebla state, near Mexico City.
Police have 72 hours from the time of the arrest to take the detainee before a judge at the national court for questioning or release him.
A court official said the detainee had been making online threats against those opposed to the Pope`s visit, and police, who had been monitoring it decided to arrest him.