Thousands attacked the complex in Kunduz but did not get in as violence flared across the city, in unrest that raised to 27 the death toll from protests at Quran burning by troops from the US-led NATO force, according to an AFP tally.
A UN spokeswoman confirmed the attack but refused to say how many UN staff were on site at the time.
It came as protests turned ugly across Kunduz with demonstrators burning tyres and traffic booths, witnesses said. There was also violence in Mihtarlam, central Afghanistan, where protesters suffered gunshot wounds.
An AFP correspondent at the United Nations complex saw police firing at protesters to try to stop them entering the premises at around 2:00 pm (0930 GMT), around four hours after the demonstration began.
Officers had so far managed to stop the crowd from entering the compound, police spokesman Sarwar Husaini told AFP, adding that reinforcements were being sent to protect the premises.
Sahad Mokhtar, head of the public health department in Kunduz, said: "The report we have so far from hospitals is three killed, 47 wounded in today's demonstrations."
There were protests in at least five different provinces of Afghanistan on Saturday over the burning of Korans -- which prisoners were allegedly using to pass messages -- at the US airbase at Bagram near Kabul.
The Quran burning has inflamed anti-Western sentiment already smouldering in Afghanistan over abuses by US-led foreign troops, such as the release last month of a video showing US Marines urinating on the corpses of dead Afghans.
Denise Jeanmonod, a spokeswoman for UNAMA, the United Nations' mission in Afghanistan, confirmed the Kunduz incident, saying that the organisation was "assessing the situation at the scene."
But she refused to give further details "for the security of staff" at the compound, or to say how many people were there.
There were also violent protests on Saturday in Mihtarlam, in the central province of Laghman, where hospital officials told AFP 15 protesters had been brought in with gunshot wounds.
Rallies elsewhere in Afghanistan were largely peaceful, however, authorities said.
A demonstrator in Mihtarlam, named only as Abdullah, put the crowd there at "around 2,000" and said: "The protesters turned violent and were throwing stones at the governor's palace.
"Gunshots were fired by the security forces."
A police spokesman in the town said: "The police have been able to disperse the demos in the city, but there are still some bunches of protesters around."
Government and local police sources in the eastern provinces of Logar and Nangarhar, and the central province of Sari Pul, said rallies were also being held in those locations but were largely peaceful so far.
In Sari Pul, demonstrator Mohammad Sadiq said "around 5,000" people had gathered at the Pul-e-Khishti mosque. "They condemned the holy Quran burning," he said. "It is not violent yet."
Authorities were not immediately able to confirm the size of the crowd.
In Logar, a police source said: "Around 200 people, mostly university students have taken to the streets in Muhammad Agha district.
"They have closed the Kabul-Logar highway, and are chanting 'Death to America' and 'Death to Karzai'."
President Hamid Karzai's government and the US-led NATO mission in Afghanistan have appealed for calm and restraint, fearful that Taliban insurgents are trying to exploit the anti-American backlash.
The circumstances surrounding the Quran incident, which happened overnight Monday to Tuesday, are still subject to investigation.
But US officials told AFP the military removed the books from a prison at Bagram because inmates were suspected of using them to pass messages.