Libya conflict: Zawiya claims denied by Moussa Ibrahim
It is 30km (22 miles) to the capital's west on the strategic road to Tunisia.
The rebels hope to capture Zawiya to essentially cut the capital off from the outside world, correspondents say.
A "small group of rebels" had been repelled by government forces as they tried to move into the town, said government spokesman Moussa Ibrahim.
Around 50 rebels had begun fighting inside the town, but they they were being rounded up, he added.
Last week, hundreds of rebels pushed out of the Nafusa Mountains in the west, down towards the towns of the coastal plain in an offensive aimed at ending months of deadlock.
Military success in the west of the country is crucial to the rebels' chances of defeating Col Muammar Gaddafi's government, which has prevented the rebels advancing from their stronghold around Benghazi in the east.
'Overjoyed residents'
Reports said some 200 rebels came under heavy shelling as they approached Zawiya on Saturday, with a group advancing as far as the town's main square despite coming under sniper-fire from rooftops.
An AP reporter travelling with the rebels said they were greeted by hundreds of overjoyed residents, and that they replaced the green flag of Col Gaddafi's regime hanging from a mosque minaret with two rebel flags.
The town is key for both the rebels and pro-Gaddafi forces, says the BBC's Matthew Price in Tripoli: the rebels need to take it before they can advance on the capital, and it is crucial to the Libyan regime as it lies on the lifeline to Tunisia.
If the government forces lose control of it, the capital is cut off, adds our correspondent.
Meanwhile, to the east of Tripoli, rebels have taken the town of Tawarga, 50km (35 miles) outside Misrata.
The commander who led the fight, Ibrahim Halbus, said 12 men had been killed freeing Tawarga, which had been used by government forces as the launchpad for long-range rockets targeting Misrata.
"Our martyrs can rest easy in their graves, and the children can sleep easy in their beds," he said.
Rebels now hope to press on to a strategic junction, just outside Tawarga, capturing which would sever the last remaining supply route to the city of Sirt, Col Gadaffi's birthplace.
Hundreds of people have been killed since the uprising against Col Gaddafi's authoritarian rule began in February.
Since March, Nato planes have been enforcing a UN-backed no-fly zone to defend civilians, with near-daily air strikes targeting army units and command-and-control centres.