MEXICO CITY — Convicted of killing a bishop who had cataloged human rights crimes during Guatemala’s long civil war, Byron Lima Oliva, an army captain, went to prison and, prosecutors said, built a powerful criminal operation from behind bars.

Yet it did not keep him safe. On Monday, Mr. Lima was attacked and killed inside the Pavón prison, Interior Minister Francisco Rivas said at a news conference. Twelve other people, including an Argentine model, Joanna Birriel, who was visiting Mr. Lima, were killed in ensuing unrest at the prison.

The police retook control of the prison late in the afternoon.

Mr. Rivas said the attack had been planned by another inmate, Marvin Montiel Marín, known as El Taquero, who is serving an 826-year sentence for a 2008 attack on a bus that killed 16 people. Mr. Montiel and Mr. Lima led rival groups in the prison that clashed over drug sales, prisoner transfers and internal discipline, Mr. Rivas said.

Mr. Lima, 46, was serving a 20-year sentence for the 1998 murder of Bishop Juan José Gerardi, who was bludgeoned in his quarters just two days after presenting a voluminous human rights report on atrocities in the civil war.

Mr. Lima’s life in the military and behind bars — where he made many enemies — offers a “key illustration” of how clandestine structures inside Guatemala’s military “have been able to morph into criminal groups that have used their connections to gain power and economic benefit, either licitly or illicitly,” said Adriana Beltrán, an expert on the country’s military networks who is a senior associate at the Washington Office on Latin America.

After Mr. Lima’s 2001 conviction, he became Guatemala’s most notorious prisoner, enjoying extensive privileges, using Facebook and even writing a newspaper column. He ran a sewing cooperative that made campaign T-shirts for the 2011 presidential campaign of former President Otto Pérez Molina, who faces charges in four corruption cases.

In 2013, Mr. Lima was stopped while traveling outside the prison, a capture that revealed he had been able to leave almost at will.

The next year, the attorney general’s office and Guatemala’s anticorruption commission, a United Nations-backed panel of international prosecutors, charged him and the director of the prison system — a former military academy classmate — with running a vast extortion scheme.

According to the panel, known as the Cicig, the racket charged prisoners for favors.

Attorney General Thelma Aldana’s office and the Cicig, which is led by Iván Velásquez, a Colombian prosecutor, have been uncovering corruption scandals that are shaking the country’s political and business power structure.

Last month, Mr. Pérez Molina and former Vice President Roxana Baldetti were accused of running a scheme to give government contracts to prominent businesspeople in exchange for illegally financing the 2011 campaign.

According to prosecutors, they received about $38 million in kickbacks.

At a pretrial hearing in the case on Monday, Judge Miguel Ángel Gálvez announced Mr. Lima’s death to the courtroom. Speaking to reporters during a break, Mr. Pérez Molina said he remained convinced of Mr. Lima’s innocence in the Gerardi murder.

Political tensions have been rising since the kickbacks case materialized. Last month, Ms. Aldana said she had received death threats and then suspended public activities before leaving the country for meetings in Colombia and the United States. She returned to Guatemala on Saturday.

Mr. Rivas said the threats might have come from some of the suspects in the corruption cases. The kickbacks case shows how “a criminal enterprise has been able to co-opt the state from within,” Ms. Beltrán said. “It has touched a lot of nerves.”