A gunman wearing an Israeli Army uniform and firing an M16 assault rifle blasted his way into the ancient Dome of the Rock Mosque here today, killing two Arab guards, seriously wounding seven persons and touching off three hours of rioting by Palestinians that injured dozens more.

Police held an American-born Jew in the shooting and identified him as Alan Harry Goodman, 30, who immigrated to Israel two years ago.

The shooting spree damaged windows and walls in the mosque, one of Islam's holiest shrines, on the Temple Mount on the eastern side of Jerusalem's Old City. It shattered the quiet of Easter morning and sent thousands of Christian pilgrims and Jews celebrating Passover fleeing from the area.

At least 30 persons were reported wounded by gunfire, some of them reportedly when Israeli security forces and border police opened fire with automatic weapons and tear-gas grenades to disperse angry, rock-throwing crowds of Palestinians who filled the 35-acre Temple Mount, brandishing knives and clubs and vowing to "redeem in blood" the honor of the mosque. Dozens more were hurt by stones, including Israeli troops and tourists who were the targets of rock-throwing protests throughout the day.

The gunman shot up the mosque with his automatic rifle until he exhausted his ammunition. About 30 minutes after the drama began, he was captured by four border policemen and Jerusalem's police commissioner, Yeshoshua Caspi.

Little was known about the gunman's motives, but Caspi, who led him out of the mosque, was quoted on Israeli radio as saying Goodman told him in English, "I had to do it. They were killing my relatives and friends, and I had to take revenge."

Police sources said they found in Goodman's room in a Jerusalem boarding house leaflets printed by the extremist U.S.-based Jewish Defense League, headed by Rabbi Meir Kahane. The league, which has sought to overturn a prohibition against Jews praying in the Temple Mount's mosques, said here that it was not involved with the attack.

Israeli Prime Minister Menachem Begin tonight deplored what he called "this terrible sacrilege" on the Temple Mount, which, he said, is a site "holy to Judaism, Christianity and Islam--in that historical order." He accused the Supreme Moslem Council, which placed the blame for the shootings on the state of Israel, of attempting to "exploit the tragedy."

In Washington, the State Department denounced the shooting as an "outrage" and said that "according to the information available to us," it was "obviously the work of a deranged individual."

United Press International quoted the Israeli news agency ITIM as saying that some of the persons injured were American tourists, but there was no official confirmation or identification of the victims.

Witnesses to the shooting in the mosque described a scene of pandemonium as panicked Moslem worshipers and tourists scrambled for cover over the face of the sacred outcropping of rock sheltered by the 7th century structure, also known as the Mosque of Omar. The rock is the site from which the prophet Mohammed, according to Islamic belief, ascended to heaven, and upon which, according to the Old Testament, Abraham prepared to sacrifice Isaac.

As Israeli security forces rushed into the mosque, some Arab worshipers began hauling oriental prayer carpets to the plaza outside to prevent them from being harmed.

As Goodman was led from the Temple Mount, protected by a cordon of police, he was surrounded by an angry crowd of Arabs who demanded that he be turned over to them. But troops firing over the protesters' heads drove the crowd back, and Goodman was hustled into a police van parked on the plaza.

Demonstrators hurled rocks at the security forces and over the top of the Western Wall of the ancient Second Temple, which, popularly known as the "Wailing Wall," is sacred to Jews.

Thousands of Jewish worshipers and foreign tourists in the plaza below the Temple Mount were evacuated, and as Arab shopkeepers throughout the Old City hurriedly began closing their shutters, thousands of Christian pilgrims who had been celebrating Easter services in the Holy Sepulchre Basilica and other churches streamed out of the gates of the walled Old City.

The incident was a violent turn in years of volatile religious conflict--with nationalist undercurrents--between Jews and Moslems over control of the Temple Mount. It is the site of the ancient Jewish temple of King David, the Dome of the Rock Mosque and the Al Aqsa Mosque, Islam's third holiest site after Mecca and Medina.

Since 1967, when Israel captured East Jerusalem and the Old City, the Temple Mount has been off limits for Jewish worshipers, partly as a result of a conciliatory gesture by the Israeli government and partly because a rabbinical decree forbids Jews to tread on the site of the Holy of the Holies, the innermost and most sacred area of the ancient Temple of Jerusalem, accessible only to the Jewish high priest.

The drama began shortly after 9 a.m. when, police said, the gunman walked up the long ramp from the "Wailing Wall" plaza to the gate opening onto the Temple Mount.

When he was stopped by two policemen, one Arab and one Israeli, he opened fire, wounding both seriously, police said. He then dashed across the broad esplanade past startled tourists to the mosque's main entrance, where he shot and killed two Arab guards, Haj Saleh Yamen, 70, and Jihad Ibrahim Azzizi, 20, authorities said.

Inside, Arab witnesses said, Goodman shot worshipers at random, seriously wounding at least five as bullets ricocheted through the cavernous shrine, crashing through brilliant stained-glass windows and damaging elaborately inscribed ceramic tiles covering the walls.

There were conflicting reports of what happened next, but some witnesses said the gunman then rushed into a small cave beneath the sacred rock, where he apparently intended to hold out. Witnesses said he was carrying a bag of supplies and a sleeping bag. The cave is where Abraham, David, Solomon and Elijah prayed, according to Jewish tradition, and where the angel Gabriel directed Mohammed to heaven, according to Islamic tradition.

Witnesses said security forces used smoke bombs to screen a small assault force led by Caspi, which subdued the gunman.

Hundreds of Arabs, not knowing what was happening, poured into the Temple Mount, many of them carrying makeshift weapons, and began stoning police and troops.

For three hours, several hundred Palestinian youths battled with security forces, stoning the combat-dressed troops and then retreating from bursts of gunfire and billowing clouds of tear gas only to regroup and advance again.

Outlawed Palestinian flags were unfurled by youths whose faces were masked by red-checked head scarfs. The demonstrators chanted "PLO, PLO, Palestine is Arab," and, "We will avenge with blood and spirit the mosque's honor."

When the protesters made one last advance on the Israeli police substation at the northernmost end of the plaza, troops again opened fire, while policemen with tear gas rushed the crowd.

Israeli authorities said the shooting was in the air to warn the protesters, but a Palestinian doctor at the scene, Rateb Abu Rameileh, of Makassad Hospital in East Jerusalem, said 30 injured persons had been taken to his hospital, many of them with gunshot wounds. Other wounded, including several border policemen and soldiers hit by rocks, were taken to Haddassah Hospital at Mount Scopus.

The Temple Mount, strewn with prayer rugs and with only a few elderly Arabs still kneeling and bowing in the direction of Mecca, was finally deserted of protesters and most of the security forces, and the entrances were locked.

Officials of the Israeli Interior Ministry said Goodman first came to Israel in 1977, registered as a temporary resident, and then in 1980, after repeated trips to the United States, registered as an immigrant.

He studied at a state-run Hebrew-language school in Jerusalem and was described by principal Jenia Gildad as a loner with a "strange stare" who was in constant conflict with other students. Goodman reportedly was expelled from the language school after hitting a member of the kitchen staff.

He apparently studied briefly in a Jewish religious seminary in the Tel Aviv suburb of Ranana, but dropped out and moved to a boarding house in the Jerusalem suburb of Beit Hakarem.Goodman was reported to have returned to the United States several times, finally checking back into the Beit Hakarem pension Wednesday night.Ada Cohen, manager of the pension, said he participated in the Passover Seder that night and then checked out this morning, saying he would "not be back for a long time."Investigators at the scene said they believed Goodman was from Tenafly, N.J., but officials later refused to discuss his background. One investigator said he believed Goodman had served an abbreviated term in the Israeli Army and was a reservist, which would have enabled him to possess an M16 rifle.

The Supreme Moslem Council of Jerusalem linked the shooting spree to threats recently made against East Jerusalem Arabs by a group known as "The Faithful of the Temple Mount," which has been seeking to overturn the ban on prayers by Jews in the mosques.