At least 37 people were killed and eight others wounded when militants with an extremist group attacked a secondary school in western Uganda, the authorities said on Saturday, in one of the deadliest terrorist attacks to hit the East African nation in years.

The armed outfit, known as the Allied Democratic Forces, attacked a school in Mpondwe, a town close to the border with the Democratic Republic of Congo, on Friday night, a police spokesman, Fred Enanga, said on Twitter. During the attack, a dormitory was burned and food in a store was looted, he said. At least eight people were in critical condition and had been hospitalized, Mr. Enanga added.

Ugandan officials said the army and the police were pursuing the attackers toward the Virunga National Park, a thick forest in neighboring Congo that is home to endangered mountain gorillas.

Three people were rescued from the scene of the attack, but six others were abducted, a military spokesman, Brig. Gen. Felix Kulayigye, said in a statement.

The attack is the worst that the group has carried out in Uganda since late 2021, when suicide bombers set off coordinated explosions in the capital, Kampala, that killed three people, sowing fears about the group’s reach and posing a vexing challenge for the Ugandan authorities.

Since then, the Ugandan government, in conjunction with the Congolese government, has launched an offensive against the Allied Democratic Forces with the aim of rooting the group out from its bases in eastern Congo.

The two governments have provided few details about the military campaign, saying only that air and artillery strikes have weakened the group. But regional observers have remained doubtful about the success of the operation, code-named Shujaa, or “Bravery,” saying that the militants have continued to wreak havoc in eastern Congo, a lush, mineral-rich region where more than 100 rebel groups have overseen a wave of massacres and widespread destruction for decades.

Experts also say that Uganda’s president, Yoweri Museveni, who has been in power for almost four decades, was using the operation to bolster his image and to secure oil fields that are being dug near the border with Congo.

The attack, which began around 11:30 p.m. on Friday, was widely condemned by lawmakers, opposition parties and Western embassies, who called on the government to institute measures to prevent such actions in the future.

“We hope that investigations can begin in earnest so that the perpetrators of this crime face justice,” Bobi Wine, a musician-turned-opposition-leader, said on Twitter.

On Saturday afternoon, photos and video shared on social media and television channels showed a heavy military presence in the area as aid workers responded to the scene of the attack. General Kulayigye, the military spokesman, said the chief of the country’s defense forces and the commander of the land forces were expected to visit the area.

Maj. Gen. Dick Olum, the commander of Uganda’s military operation in Congo, said at a meeting with residents that rebel members had spent two nights in the town before attacking the school. He said that some of the students had been burned or hacked to death, and that government pathologists would carry out DNA tests to identify the charred bodies.

The government has deployed planes to search for those who were abducted, he added. He also called on the town’s residents to remain vigilant and report anything suspicious.

The fact that this attack happened, he said, “is a very shameful thing.”

The Allied Democratic Forces was established in eastern Congo in 1995 by two groups opposed to Mr. Museveni, one of them an Islamist sect. The group also received regional backing from leaders in other countries, including Sudan and Congo, who sought to undermine Mr. Museveni’s rule.

In 1998, rebels affiliated with the group attacked a college in western Uganda, killing 80 students and kidnapping 100 others. But beginning in 2011, major offensives carried out by the Ugandans, the Congolese and United Nations peacekeeping forces undermined the group, prompting it to retreat deeper into the mountainous Ruwenzori region that borders Uganda and Congo.

The group’s former leader, Jamil Mukulu, was also captured in Tanzania in 2015 and then extradited to Uganda.

The group has nonetheless continued to carry out even more vicious attacks. Over the past few years, it has recruited new members, including children; attacked peacekeepers; conducted jail breaks; and engaged in sexual violence, according to the United Nations.

It also pledged allegiance to the Islamic State, which in 2019 claimed its first attack in Congo. In 2021, the United States designated the A.D.F. a terrorist organization and offered a reward of up to $5 million for information on the group’s new leader, Seka Musa Baluku.

But while there are some financial connections and ideological similarities between the two entities, regional observers and United Nations experts say there is no “conclusive evidence” of the Islamic State commanding or controlling the group’s operations.