An imam who masterminded the August terrorist attacks in Barcelona and a seaside resort that killed 16 people had provided information to the Spanish secret service while he was in prison in 2014, a secret service official said Friday.

The secret service contacted the imam, Abdelbaki Essati, while he was serving a sentence on narcotics charges, according to the official, who spoke on the condition of anonymity to discuss a security matter.

The official said that the imam was not a formal informant, but instead one of the contacts that the secret service, the National Intelligence Center, or CNI, cultivated to learn more about radical Islamist groups operating in Spain.

He would not say what kind of information Mr. Essati provided, or whether Mr. Essati remained in contact with CNI after leaving prison in 2014.