The authorities opened their investigation after receiving a tip that a Moroccan trafficker had met several times with two men who wished to make a significant purchase of drugs in Malaga on Spain's southern coast, police said in a statement.
When the exchange of drugs took place "five other people appeared, who were armed and wearing police vests and shirts and carrying police documents as if it was a raid," the statement said.
The five seized the drugs and tied up the traffickers who were selling it with rope and abandoned them in a desolate area, the statement added.
Police on Sunday arrested five suspects who they believe were involved in the ambush, and seized 69 kilos (152 pounds) of cannabis resin, near Malaga just as they were heading to Logroño in northern Spain where the authorities suspect they planned to sell the drugs.
Two other suspects were arrested, one in Guarromán in the south and another in Logroño.
Four policemen and a security guard were among those arrested. The suspects have been charged with drug trafficking, membership of an organized crime group, kidnapping, illegal arms possession, violent robbery and impersonating police.
Spain is a major transit point for drugs due to its location at the crossroads of North Africa and western Europe.