A candidate in Ecuador’s forthcoming presidential election who has campaigned against corruption and gangs has been shot dead at a campaign rally.

Fernando Villavicencio, a member of the country’s national assembly, was attacked as he left the event in the capital, Quito, on Wednesday.

He is one of the few candidates to allege links between organised crime and government officials in Ecuador.

A state of emergency has been declared following the assassination.

Ecuador has historically been a relatively safe and stable country in Latin America, but a recent rise in violent crime – fuelled by the growing presence of drug cartels – has been a central issue in this year’s presidential campaign.

Witnesses said Mr Villavicencio, a serving congressman and former journalist, was shot three times.

A member of his campaign team told local media the 59-year-old was getting into a car when a man stepped forward and shot him in the head.

Video from inside the building shows panicked supporters diving for cover and campaign leaflets littered across a blood-stained floor.

The suspect was also shot in an exchange of bullets with security and later died from his injuries, the country’s attorney general said on social media.

In the chaos, nine other people were injured, including a candidate for the country’s assembly and two police officers, prosecutors said.

Six people have been detained by police in connection with the assassination after raids in Quito, they added.

The first round of the presidential election is scheduled to take place on 20 August.

Mr Villavicencio, who was married and had five children, was one of eight candidates in the first round of the election – although he was not the frontrunner and was polling around the middle of the pack.

As well as security, Mr Villavicencio’s campaign had focused on tackling corruption, a topic he had covered in an earlier career as a journalist, and reducing environmental destruction.

Last week, he said he and his team had been threatened by the leader of a gang linked to drug-trafficking.

Falling between the world’s two largest cocaine-producing nations – Colombia to the north and Peru to the south – Ecuador has been used as a transit country to smuggle the illegal drug to lucrative markets in North America and Europe.

The cartels have infiltrated local street and prison gangs, unleashing a wave of violence in Ecuador’s port cities and its Pacific coast as they battle for control of strategic areas to load tonnes of cocaine on to ships and smaller vessels.

Mr Villavicencio’s killing comes in the wake of other high-profile political violence, including the murders of Agustín Intriago, mayor of the city of Manta, in July and Omar Menéndez, candidate for mayor in the city of Puerto López, in February.

Police officers work outside the rally site where Mr Villavicencio was killed

Current President Guillermo Lasso vowed the “crime will not go unpunished”.

Mr Lasso, who will not be on the ballot, said he was “outraged and shocked” by the killing, adding: “Organised crime has come a long way, but the full weight of the law is going to fall on them.”

Last month, Mr Lasso declared states of emergency and night curfews in three provinces following a number of killings linked to organised crime.

Paying tribute, Mr Villavicencio’s party, Movimiento Construye, shared a comment he posted to social media in response to calls to suspend the presidential campaign following Mr Intriago’s death.

“Hiding in moments when criminals assassinate citizens and officials is an act of cowardice,” he wrote.

Former vice-president and fellow candidate Otto Sonnenholzner sent his “deepest condolences and deep solidarity” to Mr Villavicencio’s family.

“May God keep him in his glory,” he wrote. “Our country has got out of hand.”

Frontrunner Luisa Gonzales also shared her “solidarity” to Mr Villavicencio’s family, adding: “This vile act will not go unpunished.”