What Libyan rebels had predicted would be a day of street parties celebrating their takeover of Tripoli ended in a hurried retreat by some on Monday, when a brigade sent to set up security in the city came under fierce attack by forces loyal to Moammar Gaddafi.

The rebels’ arrival in the capital Sunday met little resistance, and by Monday, amid reports that some Gaddafi loyalists had laid down their weapons, opposition forces were in control of most parts of the city. But the clash, along the shores of the Mediterranean near the center of town, and reports of sniper fire across Tripoli made clear that the rebels’ control is far from complete.

As part of a detailed opposition plan to reestablish security, an elite rebel brigade was dispatched to secure key positions in Tripoli, including the port, government ministries and the national museum.

From his new base, at what had until days ago been a women’s military academy, a top rebel commander boasted Monday afternoon that his units had laid the groundwork for liberating Tripoli.

The rebels had smuggled weapons into the city by boat, secretly bought machine guns from Gaddafi forces and organized an underground resistance network, which was “activated” when opposition fighters marched on the capital, said Emhemmed Ghula, deputy commander of the rebels’ Tripoli military council.

Those preparations had paid off, he said. “We are currently in control of 90 percent of the capital,” said Ghula, his spotless white robe and gold-embroidered vest forming a sharp contrast with the appearance of his fighters — bearded men with machine guns in their hands and dusty sandals on their feet. “We rule the streets now.”

Inside the waterfront compound, the rebels had ripped up posters of Gaddafi. A group of fighters was fixing an antiaircraft weapon mounted on a pickup truck, and the mood was decidedly optimistic. Some younger men flashed victory signs for journalists as others napped in the dried-out garden of the military academy.

Their rest was interrupted by a sudden hail of bullets that zinged through an open steel gate. The rebels scrambled to their feet, running around in disarray and shooting at anything that moved in the distance.

Pro-Gaddafi forces had mounted an all-out assault on the rebel base, blasting into the compound as snipers from nearby high-rises took aim at people in the academy.

After half an hour of intense fighting, the rebels seemed to have at least slowed down the surprise attack. But it was clear that the base was no longer safe. “We are going to leave this location,” Ghula said, as his men prepared to move to a safer neighborhood.

At another makeshift opposition base in the city, a mob of rebels tried to attack Hala Misrati, a state television anchor who, over the weekend, waved a handgun while she was on the air and promised to fight for Gaddafi.

Libyan rebels had detained her earlier in the day, and, at the base, a crowd of about 50 men barged into the hallway demanding to see Misrati, one of Gaddafi’s most prominent mouthpieces in recent years.

Misrati, whose brother also was detained, could be heard shouting that she was innocent and that Gaddafi had lied to her. One man called her a traitor, and a rebel commander fired warning shots to disperse the mob.

“She is being treated very well,” said Abdul al-Afiz, a former air force member who had joined the rebels. He refused to show Misrati to the reporters who were present, but he said she had seen the errors of her ways. “She is very nervous,” he said. “She told us that she didn’t know that we are the good guys.”