LAGOS, Nigeria (AP) _ Forty-two men convicted in a bloody coup attempt were executed by firing squad Friday night immediately after appeals of their death sentences were rejected, the government announced.

The echoes of rifle fire were heard outside Kirikiri Prison, where the condemned men were held. Many prominent Nigerians and international organizations, including Amnesty International, had urged President Gen. Ibrahim Babangida to spare their lives.

They were charged with attacking Dodon Barracks, the military headquarters and residence of Babangida, on April 22 in an attempt to overthrow him. Babangida barely escaped with his life, and his house was badly damaged.

The coup leaders, who took over national radio headquarters for several hours, said they wanted to end the domination of the country by Moslem northerners.

The men were convicted following a military tribunal this month that was closed to the public, and they were sentenced to death. The Armed Forces Ruling Council rejected appeals of the death sentences, and the executions were carried out, the radio reported.

The council ordered retrials for 31 other people convicted by a military tribunal.

The council met Thursday and Friday, making its ruling late Friday afternoon. There was little doubt the coup ringleaders, including Maj. Gideon Orkar, would be shot. Wooden stakes had been set up at the prison where the men were held.

Hundreds of people, including some civilians, were arrested after the coup attempt. The government said the civilian financer of the coup, millionaire businessman Chief Great Ovedje Ogboru, and some officers believed involved in it, had escaped.

Adm. Augustus Aikhomu, the No. 2 man in the military government, read a statement on television and radio Friday evening saying 764 people had been acquitted of all charges.

No casualty figures were ever released from the attempted overthrow, but army spokesman Col. Fred Chijuka said it was ''the bloodiest'' attempt ever against the government.

Dozens of people died in coups in 1966, which triggered ethnic violence that ultimately sparked the Biafran War. The 1967-1970 war was an effort by easterners to secede from the federation. The easterners, mostly Christian or followers of traditional African religions, alleged Moslem northerners dominated the nation.

The rebels, after capturing the national radio station in April, made a similar claim. They also said Babangida's execution of Lt. Gen. Mamman Vatsa in 1986, for allegedly plotting a coup, was a reason for seeking his overthrow.

Ethnic tensions were fanned in January when Babangida removed Lt. Gen. Domkat Bali from his post as defense minister, effectively demoting the most important non-northerner in the government.

Though Bali said he remained loyal to the president, there were widespread protests of his reassignment, and the general retired.

The government sought to portray the coup attempt as a bid by greedy soldiers to gain wealth. It said coup backers offered $1,428 to enlisted men and $7,142 for officers.

Fighting began at 2 a.m. with an attack on Dodan Barracks. Babangida fled and directed a counterattack. Fighting continued for 10 hours and was only quelled after senior commanders in other regions of the country went on the radio to announce their support for Babangida.