A presidential candidate in Ecuador who had been outspoken about the link between organized crime and government officials was assassinated Wednesday evening at a political rally in the capital, just days before Ecuadoreans vote in an election that has been dominated by concerns over drug-related violence.

The candidate, Fernando Villavicencio, a former journalist, was gunned down outside a high school in Quito after speaking to young supporters. A suspect was killed in the melee that followed, and nine other people were shot, officials said.

“When he stepped outside the door, he was met with gunfire,” Carlos Figueroa, who worked for Mr. Villavicencio’s campaign and was at the rally, said of the candidate. “There was nothing to be done, because they were shots to the head.”

Mr. Villavicencio, 59, was polling near the middle of an eight-person race. He was among the most vocal candidates on the issue of crime and state corruption.

It was the first assassination of a presidential candidate in Ecuador and came less than a month after the mayor of Manta, a port city, was fatally shot during a public appearance. Ecuador, once a relatively safe nation, has been consumed by violence related to narco-trafficking in the last five years.

“Outraged and shocked by the assassination,” President Guillermo Lasso wrote on the social media platform X, formerly known as Twitter, late Wednesday, blaming the death on “organized crime.”

The national prosecutor’s office said on the same platform that a suspect had been shot and apprehended amid crossfire with security forces, and had died shortly afterward. The office later said the authorities had carried out raids and detained six people in connection with the assassination.

The nine other people shot included two police officers and a candidate for a National Assembly seat, according to the prosecutor’s office. There was no immediate information about the condition of the nine people; it was unclear late Wednesday night whether any of them had died.

The killing is a major blow to a nation that was already suffering deep economic, social and political upheaval.

“Electorally speaking, this year is the most violent in our history,” said Arianna Tanca, an Ecuadorean political scientist. “I think that what is going to change is the way we conceive of politics. I think that from now on it becomes a high-risk profession.”

Ecuador, on South America’s western edge, witnessed an extraordinary transformation between 2005 and 2015 as millions of people rose out of poverty, riding the wave of an oil boom whose profits were poured into education, health care and other social programs.

But more recently, the country has been dominated by an increasingly powerful narco-trafficking industry. Foreign drug mafias have joined forces with local prison and street gangs, unleashing a wave of violence unlike anything in the country’s recent history. Homicide rates are at record levels.

Today, the violence is often horrific and public, meant to induce fear and exert control: There are regular reports of car bombings, beheadings and children being gunned down outside their schools.

Complicating the situation, Mr. Lasso disbanded the country’s opposition-led National Assembly in May, a drastic move he made as he faced impeachment proceedings over accusations of embezzlement.

The move, which is allowed under the Constitution, meant that new elections for president and legislative representatives would be held. The vote in which Mr. Villavicencio was supposed to compete is set for Aug. 20.

Mr. Villavicencio, who had worked as a journalist, activist and legislator, gained prominence as an opponent of correísmo, the leftist movement of former President Rafael Correa, who served from 2007 to 2017 and still holds major political sway in Ecuador. A presidential candidate who has Mr. Correa’s backing, Luisa González, is leading in the polls.

Mr. Villavicencio wrote often about alleged corruption in the Correa government, which made him the subject of legal persecution and death threats. He briefly sought political asylum in Peru.

In 2017, Mr. Villavicencio successfully ran for a seat in the National Assembly, where he served until the legislature was dissolved by Mr. Lasso.

Mr. Correa, writing on the X platform late Wednesday, lamented Mr. Villavicencio’s death. “Ecuador has become a failed state,” he wrote. “My solidarity with his family and with all the families of the victims of violence.”

Grace Jaramillo, an Ecuadorean professor of political science at the University of British Columbia who went to university with Mr. Villavicencio, remembered running against him in an election for student body president. He ran as a Trotskyist, and she represented a party called Democracy in Our House; both lost, to a student representing the Chinese Communist Party.

“He was really a fighter all the time and very good at arguments,” Ms. Jaramillo said. “An arguer, a challenger. He used to love lively discussions.”

After university, she said, Mr. Villavicencio became a union leader at Petroecuador, the country’s national oil company. Soon after Mr. Correa came to power, he started writing about government corruption as a political journalist.

Ms. Jaramillo said she met with him at the time to give him advice. His house had been raided, and he had no money to fight the charges that had been brought against him, she said.

“He was downtrodden. He felt bullied and diminished,” she added.

But a few weeks ago, when she saw him on a trip to Quito, he was “really hopeful and enthusiastic,” Ms. Jaramillo said. “He was convinced that he could make it to the second round” of the presidential election.

His death, she said, will be “a long-lasting memory of how difficult it is to fight corruption and to be safe at the same time.”