News reports showed an angry crowd outside the consulate building and clashes broke out with security forces sent to protect the building.
Lebanese leaders condemned the attack and Interior Minister Hassan Sabeh announced his resignation.
The violence came a day after mobs in neighbouring Syria torched the Danish and Norwegian embassies in Damascus.
Danish Foreign Minister Per Stig Moeller earlier called for a calming of tensions.
The Danish government on Sunday called on its nationals to leave Lebanon, saying they "should remain indoors until the travel possibilities have been clarified".
Twenty-eight people were wounded in the Beirut clashes, some were treated for inhaling teargas and others had sustained fractures, a civil defence medic said.
"While I understand that many Muslims have been offended by the recently published editorial cartoons, there is absolutely no justification for this violence"
Jaap de Hoop Scheffer, Nato Chief
Lebanese security forces had fired tear gas into the crowd of 20,000 who marched through a Christian neighbourhood before arriving at the Danish mission
The protesters attacked property and shops in Ashrafiyeh district, throwing stones, breaking windows and overturning cars, a journalist said.
There were chaotic scenes as hundreds as protesters marched through the area, knocking down barricades. Stones were thrown at a church as Islamic leaders were seen appealing for calm.
Uproar over the cartoons, which first appeared in a Danish newspaper and were then reprinted in a number of other European countries, has swept across the Muslim world.
Aljazeera's correspondent in Beirut, Abbas Nasir, reported that Lebanese police retreated in the face of the crowd.
The police fired heavily in the air to disperse protesters who nevertheless continued to advance towards the Danish consulate.
Policemen fired tear gas in an unsuccessful attempt to prevent demonstrators from assembling in front of the building, Nasir said.
At around 11am local time (0900 GMT) on Sunday, demonstrators amassed in Ashrafiyeh, where the Danish consulate is located, throwing rocks and breaking windows.
The crowds gathered in Beirut's
Christian al-Asharafiya suburb
The Lebanese press said that Danish diplomats had evacuated the premises on Saturday night after a similiar protest in neighbouring Syria, in which mobs attacked the buildings housing the Danish and Norwegian embassies in Damascus.
As demonstrators shouted and ran through the streets of east Beirut's Christian sector, cars were overturned and their windows smashed and some people picked up stones from the street and hurled them at nearby buildings.
Journalists on the scene estimated that several thousand people were among the angry crowds, many of whom turned out in response to a call by a group called the National Movemment for the Defence of the Prophet Muhammad.
Witnesses told Reuters that security forces fired shots in the air and used water cannon on Sunday to disperse the protesters, who burned two civil defence vehicles, a police car and an army jeep.
Syrians stormed two embassies
on Saturday and torched them
Chanting "No God but Allah, Muhammad is His Prophet", the protesters threw stones at the security forces. A number of people fainted because of the tear gas.
Security forces sealed all roads leading to the embassy in the eastern part of the Lebanese capital. Islam forbids any depiction of the Prophet Muhammad.
Gunfire could also be heard and witnesses said troops were firing in the air to keep the protesters away.
About 2000 army troops and riot police were deployed around the embassy building in the district of al-Ashrafia, close to Beirut's commercial district.
The protesters, who came in buses from all over Lebanon, waved green Islamic flags and banners, some of them reading: "O nation of Muhammad, wake up"
Muslim clerics were seen among the protesters.
Some witnesses said the protesters set fire to several cars and threw stones at a nearby church.They also burned several Danish flags.
Witnesses said at least two protesters fainted, overcome by the tear gas, and were taken away in ambulances.
Palestinians too have protested
against the cartoons
The security official, who spoke on condition of anonymity because he was not allowed to speak to the press, said embassy staff had evacuated the building along with their belongings and documents two days ago, in anticipation of protests.
The violent protest comes a day after thousands of protesters in neighbouring Syria set fire to the Danish and Norwegian embassies in the most violent in days of furious protests by Muslims in Asia, Europe and the Middle East.
The Scandinavian countries and the US condemned Syria for failing to protect the embassies.
Officials at a meeting of the world's top defence officials appealed on Sunday for calm and urged respect for both religious and press freedoms in the wake of the violent incidents.
"We must prevent a situation arising where people think we must choose between these two liberties," Frank-Walter Steinmeier, the German foreign minister, told participants.
"They must be respected not just in Europe but around the world."
Robert Zoellick, the deputy US secretary of state, decried the "terrible violence" - but said "undoubtedly the cartoons were offensive and insensitive to religious beliefs".
The crowd also attacked a church
and other properties in Beirut
"The role here of people who disagree is to criticise, to debate, but certainly not to turn to violence," he said.
Jaap de Hoop Scheffer, the Nato chief, issued a statement strongly condemning the attacks on the embassies in Syria.
"While I understand that many Muslims have been offended by the recently published editorial cartoons, there is absolutely no justification for this violence, nor can the freedom of the press in our countries ever be called in any way into question," he said.
"I express my full solidarity with the governments of the countries which have suffered these attacks."