An Afghan police recruit has shot dead two Spanish police officers and their interpreter in Badghis province, north-western Afghanistan, officials say.
The assailant opened fire during a training session in a premeditated attack, according to Spanish Interior Minister Alfredo Perez Rubalcaba.
Security forces returned fire and killed the police recruit.
After the shooting, a crowd of angry locals violently protested outside a Spanish base in Qalay-i-Naw.
The BBC's Quentin Sommerville in the capital, Kabul, says hundreds of men attacked the base shouting and throwing stones.
Some attempted to scale the walls and shots were also fired, our correspondent says.
The protest has now ended, and a local hospital spokesman said 24 people had been injured - some with bullet wounds.
The two Spanish officers were from the paramilitary Civil Guard.
"In a class, one of the students apparently opened fire on the two Civil Guard policemen and the interpreter, who was also Spanish, and killed all three," Mr Rubalcaba told the Cadena Ser radio station.
"The security forces in turn repulsed the attack, fired on the assailant and killed him."
He said the Spanish authorities were looking into who ordered the attack.
The Taliban says the cadet was working for them, but the militant claims could not be verified.
"I can't say whether the Taliban was behind it or not, [but] what is clear is that it was a premeditated attack... it was a terrorist attack," Mr Rubalcaba said.
The Nato-led Isaf mission to Afghanistan said earlier that the motive for the killing was not clear.
A spokesman for the governor of Badghis, Sharaf Uddin, told the BBC's Pashto service that the killings happened after an Afghan policeman had quarrelled with the two Spanish officers.
Such rogue attacks are rare, but our correspondent says they do damage morale.
Previous shootings of international personnel by Afghan recruits include an incident in which a Afghan soldier killed three British troops in Helmand province last month.
Spain currently has 1,555 personnel deployed in Afghanistan.