The crowd also shouted "Gyanendra thief, leave the country" as a power shutdown plunged the area into darkness, Ishwar Dhakal, a witness said.
Police said they charged the crowd with batons before the king was driven back to the palace safely.
Gyanendra has been isolated since he gave in to mass protests last year, stripped of most of his powers including the control of the army, and is rarely seen in public.
Gyanendra was a businessman before he became king in 2001 after his brother, Birendra, and eight other royals were shot dead in a drug-and-drink fuelled shooting spree by the then crown prince, who later turned the gun on himself.
Friday's attack came despite authorities deploying thousands of policemen at the pilgrimage site over fears that religious groups could use the festival to demand the country revert to being a Hindu state.
The Himalayan nation was the world's only Hindu state until it was declared a secular country last year after Gyanendra stepped down from power.
|Nepal's king was traditionally regarded as an|
incarnation of the Hindu god Vishnu [AFP]
Organisers of the festival said about 300,000 pilgrims from Nepal and neighbouring India were expected at the temple of Lord Shiva, one of the trinity of Hindu gods.
Hundreds of "sadhus", or Hindu holy men, were also expected for the celebrations which continue through the night.
Local media reports said some religious groups were planning protests to demand Nepal - about 80 per cent of whose 26m people are Hindus - return to being a Hindu state.
Baman Prasad Neupane, a senior home ministry official, told Reuters: "Some people could create trouble. The government is fully prepared to stop anyone who tries to indulge in violence."
About 2,000 policemen would be on guard and more forces were on standby for deployment in the event of trouble at one of Hinduism's holiest sites, police said.
The Pashupati Area Development Trust, a religious body that organises the festival, urged pilgrims to refrain from politics inside the temple.
In a statement, it said: "All activities other than religious, cultural and traditional have been banned from the temple premises."