The temple replaces a 16th-Century mosque razed by Hindu mobs in 1992. The demolition had sparked nationwide riots in which nearly 2,000 people died.
Thousands of invited guests, including top film stars and cricketers, attended the event in Ayodhya.
But some Hindu seers and most of the opposition boycotted it, saying Mr Modi was using it for political gains.
General elections are due in India in the next few months and Mr Modi’s political rivals say the governing Bharatiya Janata Party (BJP) will be seeking votes in the temple’s name in a country where 80% of the population is Hindu.
Critics have also accused the government of going overboard with what is essentially a religious celebration in a country which – according to its constitution – is secular.
The temple has been constructed at a cost of $217m (£170m) – the temple trust says it’s been funded from private donations.
The ceremony, which was televised live, showed Mr Modi performing religious rituals inside the temple’s sanctum along with priests and Mohan Bhagwat, head of Rashtriya Swayamsevak Sangh (RSS) – the ideological fountainhead of Hindu nationalist parties. Only the ground floor of the temple was opened – the rest of the structure is expected to be completed only by the end of the year.
The ceremony, called Pran Pratishtha, which loosely translates from Sanskrit into “establishment of life force”, lasted about an hour. Hindus believe that chanting of mantras and a set of rituals performed around a fire will infuse sacred life in an idol or a photograph of a deity.
The temple fulfils a decades-long Hindu nationalist pledge to build a shrine to Lord Ram in Ayodhya – the flashpoint city is getting a major makeover to transform it into a destination for pilgrims and tourists from across the world.
Many Hindus believe that Ayodhya is the birthplace of Ram and the Babri mosque was built by Muslim invaders on the ruins of a Ram temple at the exact spot where the Hindu god was born. The movement to build a temple at the same site was a major factor that propelled the BJP into political prominence in the 1990s.
A protracted legal battle about the ownership of the land followed the mosque’s demolition. It was resolved in 2019 when the Supreme Court gave the disputed land to the Hindus. Muslims were given a plot outside the city to build a mosque.
In Ayodhya, some Muslims told the BBC before the event that the day evoked fear and painful memories for them. Some said they would send their children out of the city, fearing tensions might be stoked when the streets filled with Hindu devotees from around the country.
The new three-storey temple – made with pink sandstone and anchored by black granite – stretches across 7.2 acres in a 70-acre complex. A 51-inch (4.25-ft) statue of the deity, specially commissioned for the temple, was unveiled last week. The idol has been placed on a marble pedestal in the sanctum sanctorum.
Since Monday morning in Ayodhya, all roads have led to the new temple. Thousands of policemen have been deployed to ensure security and manage traffic. Saffron-coloured flags of the BJP and those with images of Hindu gods dot major roads, many of which have been decorated with bright yellow and orange marigold flowers.
Some of India’s biggest celebrities, including Bollywood star Amitabh Bachchan and cricketer Sachin Tendulkar, were among those who attended the event.
To avoid crowding in the small city, Mr Modi had appealed to pilgrims to not turn up in Ayodhya in large numbers but to watch the ceremony live on television. To facilitate that, all the BJP-ruled states have shut schools and colleges and declared a full or a half-day government holiday and stock markets are shut to encourage people to watch the ceremony.
The atmosphere is celebratory among Hindus in many northern Indian cities. Mr Modi has asked people to light up lamps and party supporters have put up saffron flags with images of Ram on their rooftops, including in several Delhi neighbourhoods. Film theatres are screening the event, and big television screens have been set up at town squares and in residential colonies.
Mr Modi had called the temple a major achievement and said that the country was “eagerly waiting” for its opening.
“Many generations had looked forward to this moment,” he said in a message earlier this month, adding that he would be “representing all of India’s 1.4 billion people” at the consecration ceremony.
But a sour note was struck with some top religious seers saying that as the temple was not yet complete, it was against Hinduism to perform these rituals there, and many opposition leaders deciding to stay away.
Some of the opposition-ruled states have also announced their own plans for the day – West Bengal Chief Minister Mamata Banerjee said she would pray at the iconic temple to goddess Kali in Kolkata and then lead an all-faith rally. The eastern state of Odisha (Orissa) unveiled huge plans to bring pilgrims to the Jagannath temple in Puri, one of the holiest sites for Hindus.
Authorities say they expect more than 150,000 visitors per day once the temple in Ayodhya is fully ready.
To accommodate this expected rush, the city has been the site of frenzied construction work for months. In recent weeks, a brand new airport and a railway station have been opened, several new hotels have been built and the existing ones have been spruced up.
The government has allotted $3.85bn (£3.01bn) for the transformation of Ayodhya, the tranquil pilgrim town on the banks of Saryu river, a tributary of the Ganges.
Officials say they are building a “world-class city where people come as pilgrims and tourists”, but many local people have told the BBC that their homes, shops and “structures of religious nature” have been either completely or partially demolished to expand roads and set up other facilities.