Republican presidential nominee Donald Trump has arrived in Mexico, making a surprise visit to meet the president of a nation he derided at the start of his White House campaign as a source of rapists and criminals coming to the United States.

Trump's visit on Wednesday follows an invitation from President Enrique Pena Nieto, who has repeatedly criticised Trump's immigration and border policies and compared him to Adolf Hitler.

It came just hours before Trump was set to give a major speech on immigration in the border state of Arizona.

After a closed-door meeting, Pena Nieto told reporters that the "Mexican people have been hurt" by comments made by Trump.

But he added that he was sure that Trump is genuinely interested in building a relationship that will benefit both countries.

Trump, for his part, said he and the president had discussed his call for a border wall during their meeting, but did not talk about Trump's insistence that Mexico pay for it. He said, "That'll be for a later date."

Al Jazeera's John Holman, reporting from Mexico City, said "both were keen to appear statesman-like. The rhetoric that Trump had in the past about Mexicans being rapists was very much abandoned."

Protesters gathered in Mexico City at the famous Plaza de San Angel statue, while other Mexicans vented their anger over the visit on social media.

Both a former Mexican president and first lady bluntly told Trump that he was not welcome, despite Pena Nieto's hospitality.

Former Mexican President Vicente Fox called the trip a "political stunt".

Margarita Zavala, wife of former president Felipe Calderon, also wrote on Twitter: "We Mexicans have dignity, and we reject your hate speech."

After saying during his Republican primary campaign that he would use a "deportation force" to expel all of the estimated 11 million people living in the United States "illegally", Trump suggested last week he could soften that stance.

But he still says he plans to build a huge wall - paid for by Mexico - along the two nations' border.

Trump announced on Tuesday night that he would make the Mexico visit, accepting at short notice an invitation offered last week by Pena Nieto, who also invited Democratic candidate Hillary Clinton.

Campaigning in Ohio, Clinton jabbed at Trump's Mexican visit as she promoted her own experience working with foreign leaders as the nation's chief diplomat.

READ MORE: Will Trump's minority outreach work?

"People have to get to know that they can count on you, that you won't say one thing one day and something totally different the next," she told the American Legion in Cincinnati.

"And it certainly takes more than trying to make up for a year of insults and insinuations by dropping in on our neighbours for a few hours and then flying home again."

Trump has promised, if elected, to deport millions of immigrants who are in the United States illegally, force Mexico to build a wall to secure the 3,000km border and renegotiate the NAFTA trade agreement to make it more favourable to the United States.

He responded to Vicente Fox's criticism on Twitter, saying the former president had, like Pena Nieto, invited him to come.

Fox shot back with a tweet of his own, saying he had invited Trump to "come and apologise to all Mexicans. Stop lying! Mexico is not yours to play with. Show some respect."

Pena Nieto has been sharply critical of Trump's immigration policies, particularly the Republican's plans to build a wall and have Mexico pay for it.

In a March interview, he said that "there is no scenario" under which Mexico would do so and compared Trump's language to that of dictators Adolf Hitler and Benito Mussolini.

He had a different tone late on Tuesday, tweeting: "I believe in dialogue to promote Mexico's interests in the world and, principally, to protect Mexicans wherever they are."

Source: Al Jazeera News and agencies