PM Modi, on a day-long visit to the state to attend various public functions, earlier landed in Mangaluru, about 80 km from Dharmasthala, and flew in a helicopter to this temple town.
The prime minister waved to the crowd, who chanted "Modi, Modi", as he stood at the entrance, before proceeding to offer prayers at the temple.
He was accorded a traditional welcome at the temple, where Dharmadhikari (hereditary administrator) D Veerendra Heggade took him around.
"I am happy that I got the opportunity to pray to Lord Manjunatha," PM Modi was quoted as saying in a tweet by the prime minister's office.
PM Modi visited Dharmasthala and nearby Ujire at the invitation of Mr Heggade.
At Ujire, the prime minister attended a function of the Shri Kshetra Dharmasthala Rural Development Project, where he distributed Rupay cards and launched the 'Digitised SHG (self-help group) Member Transaction' and 'Preserve Mother Earth' campaigns.
The Dharmasthala temple is one of the renowned religious landmarks of south India with a history of over 800 years.