About 30 young Afghan students and workers, carrying bricks as weapons, took over the Afghan Embassy here for 4 1/2 hours today and then left peacefully, chanting Islamic slogans.

None of the embassy's top officers was present when the attackers scuffled with guards, broke through the glass front door and rounded up 13 employees and residents.

The captives were taken to the fourth floor of the simple gray brick building, where they could be seen through glass windows cheerily waving to the large crowd of onlookers and reporters below.

The Afghan attackers, most of them Islamic students at a theology school in the holy city of Qom, had planned to seize the embassy the night before. They wanted to search for documents that they believe show that diplomats spied on their activities in Iran and sent dossiers on them to their native country, according to a spokesman Hamid Mahajeri.

Mohajeri, 22, said he believes the information Afghan diplomats send home results in arrests of Afghan students once they return to their native land.

Although the embassy captors said they are affiliated with Moslem guerrillas in Afghanistan who are fighting Soviet troops who back the new pro-Moscow government, they said today's takeover was primarily concerned with looking through embassy files to find "embassy spies."

The embassy captors pulled down the Afghan flag, hoisted up a green Islamic flag and placed posters on the sides of the building showing lessons of the Koran.

Mohajeri allowed newspaper reporters into the occupied embassy to interview him shortly after the takeover.

Three hours after the seizure, Iranian police and a dozen revolutionary militiamen entered the building and began negotiations that resulted in the retreat of the Afghan captors a short time later.