NEW DELHI — As violence erupted in the streets Tuesday over a citizenship law that critics say discriminates against Muslims, President Trump defended Prime Minister Narendra Modi’s record on religious tolerance as he closed out a two-day visit to India that served as a celebration of the bond between two nationalist leaders.

Trump avoided any public criticism of his host — whose political biography is also built partly on religious and cultural divisions — along with any comparison of Modi’s policies with his own treatment of Muslim immigrants.

“We did talk about religious freedom, and I will say that the prime minister was incredible in what he told me. He wants people to have religious freedom and very strongly,” Trump said during a news conference that capped the public portion of his largely ceremonial visit.

Modi, who leads the Hindu nationalist Bharatiya Janata Party, has insisted that the citizenship law aims to help persecuted religious minorities in three nearby countries. But critics say it is unconstitutional and discriminates against India’s Muslim minority.

At least 17 people were killed, including a police officer, in clashes in the capital city during Trump’s visit. The violence began when supporters of the citizenship law confronted opponents of the measure.

A heavy police presence in the affected areas failed to stem the violence. Large gatherings were banned, subway stations in the surrounding areas were shut, and schools remained closed. Images of billowing smoke from gutted vehicles and shops were shared widely on social media. Several journalists were attacked.

The U.S. State Department has repeatedly expressed concerns about the crackdown in Muslim-majority Kashmir after Modi revoked its autonomy. The citizenship law, along with India’s other actions, has drawn bipartisan criticism in Congress, but Trump tread lightly.

The president said he had “asked that question” during a closed-door meeting Tuesday, but he was vague about whether he criticized Modi for his decision.

“I don’t want to discuss that,” he said when asked for his opinion of the law. “I want to leave that to India, and hopefully they’re going to make the right decision for the people.”

He did not elaborate, but that may have been a reference to admonitions from the State Department and elsewhere that India should abide by its secular constitution and refrain from appearing to promote one religious or ethnic group over another.

Protests have broken out across the country since the law was passed in December. It creates a fast track to citizenship for undocumented immigrants belonging to six religions, barring Islam.

The flowering of Trump’s admiration and affection for Modi appeared to be the main achievement of the packed schedule that took Trump to Modi’s home region and the Taj Mahal before arriving in New Delhi.

There was also relief that the visit came off without major gaffes or embarrassment on either side.

Trump has a history of errors related to India and its neighbors. He once surprised Modi by asserting that India does not share a border with China (it does). He publicly mocked India, which has committed billions of dollars to the reconstruction of Afghanistan, for building only a “library” there (he possibly mistook the Afghan parliament building, which India funded, for a library).

Trump joked at the outset of the news conference Tuesday that he would “be not at all controversial because I don’t want to blow the two days plus two days of travel on one answer.” He mistakenly said that India’s population is larger than China’s, but he otherwise got through the 45-minute question-and-answer session without much issue.

Trump’s good mood was evident throughout, buoyed by celebratory events in his honor and the warmth of his relationship with Modi, a fellow nationalist iconoclast.

Instead of the glowering figure Trump sometimes cuts overseas, he appeared cheerful in most public appearances, and he seemed moved by the more solemn occasions, such as a wreath-laying Tuesday in remembrance of Indian independence leader Mohandas Gandhi.

In one of the most striking scenes of the trip, Trump and first lady Melania Trump lofted handfuls of flower petals above the memorial, filling the air with a cloud of red.

“This is not from me. This is from just about everybody that saw, they saw that in the history of India, which has a long history and a brilliant history in so many different ways, there’s never been a reception given to somebody like was given” to him, Trump said. “And I would like to say for the United States of America that nobody else that came here got the kind of reception we have,” Trump said, touting the size of crowds that turned out to see him in Ahmedabad and New Delhi.

Trump is the fourth consecutive U.S. president to visit India while in office.

“It was a great compliment to our country” to be received so warmly, he said.

The bonhomie and praise for Modi’s leadership was all the more striking for its contrast with an apparent communal riot between Hindus and Muslims.

An image of a Muslim man bloodied and surrounded with stick-wielding men shared by ­Reuters went viral. India is a ­Hindu-majority country but has the second-largest population of Muslims in the world.

Modi came to prominence as the state chief of Gujarat, where some 1,000 Muslims were massacred on his watch in 2002. The incident, in which Modi denies culpability, led to his being refused a U.S. visa for years. A panel appointed by the Indian Supreme Court ruled that there was no evidence to charge Modi with a crime, but the incident has hung over his career.

As a 2016 presidential candidate, Trump called for a “Muslim ban” on immigration to the United States, and he branded nearly all terrorism as “radical Islamic terrorism,” a phrase he used to Modi’s approval during his visit here.

Trump and Modi issued joint statements praising the growing defense cooperation between the two countries, but they did not field questions together. They acknowledged that a trade deal between the two nations remains distant.

“We’ve had a couple of really great days in India,” Trump said Tuesday.

“Prime Minister Modi is a terrific man, he’s a terrific leader, and we have a lot of things going in terms of products being purchased by India.”

Modi was not there to hear the compliment. Although they spent much of the day together, including at a state banquet of salmon and grilled goat, Modi is known for not having held a news conference in his six years as prime minister.

Earlier Tuesday, Trump and Modi claimed progress toward a comprehensive trade deal.

Trump predicted that a successful deal is in the offing, but he gave no deadline. The White House had once hoped to have the agreement ready in September, when Modi visited the United States.

Trump said Tuesday that the pact would probably wait until the end of this year, meaning after the U.S. presidential election.

Modi said he and Trump had talked trade and had agreed “to take it forward and give it legal shape.”

“We have also agreed to initiate discussions for a bigger deal,” Modi said, through an interpreter. “We are confident that this will be a good result that will be in our mutual interest.”