President Trump and Mayor Zohran Mamdani met behind closed doors at the mayor’s residence in New York City on Monday. And as with their two previous meetings at the White House, the discussions seemed to have been warm, respectful and perhaps even productive.

Emerging together after the roughly one-hour meeting, they chatted closely and informally before walking slowly to a lectern with the mayoral seal and beginning a joint news conference that the president had not been scheduled to attend.

Mr. Trump’s meeting with Mr. Mamdani, a democratic socialist, came after a series of victories in New York and around the country by leftist candidates, whom the president and his fellow Republicans continue to deride as communists and extremists.

Yet Mr. Trump and the mayor, ideological opposites in just about every way, remained deferential to each other. The president reiterated a previous statement that he hoped Mr. Mamdani would be a “great mayor,” and Mr. Mamdani said he was ready to work with anybody, including Mr. Trump, to help New Yorkers.

Mr. Trump, a native New Yorker who is deeply unpopular in the city, characterized the visit to the mayoral residence, Gracie Mansion, as a sort of homecoming.

But the news conference also showed the downside of the political theater that Mr. Mamdani has, so far, successfully crafted with Mr. Trump. The mayor was placed in the awkward position of standing silently and expressionless as Mr. Trump denigrated media operations as “fake news,” defended his order to ban CNN, Politico and MS NOW from the White House grounds and characterized his narrow victory in 2024 as a landslide.

Asked if he challenged the president on the media ban, Mr. Mamdani said he had told the president that all members of the media would be allowed on the mansion lawn for the news conference. Answering the same question, Mr. Trump said he had “an obligation” to ostracize journalists who he feels do not give him adequate credit for what he considers his accomplishments.

Mr. Mamdani said that the meeting arose from an invitation he extended to the president during a private conversation some time ago, and that he had hoped to use the discussion to advance his affordability agenda.

They met in the mansion’s Yellow Parlor, a room decorated with Federal-era cabinetry, the two seated in front of an unlit fireplace. The president’s chief of staff, Susie Wiles, and the mayor’s own top adviser, Elle Bisgaard-Church, sat opposite the two men.

The mayor said the discussion had been “wide-ranging” and had touched on policing, immigration issues and the possibility of building a huge affordable housing development in Queens.

That potential project — calling for 12,000 housing units to be built above a multibillion-dollar platform atop Sunnyside Yard — was first broached when the two met in February at the White House. Such a project would most likely require substantial financial support from the federal government.

Mr. Mamdani, without providing details, said that he and the president had discussed their respective administrations taking the project to “the next level,” though it was unclear what that meant practically. Mr. Trump did not disagree.

“Sunnyside is a big job,” the president said, adding that the idea had been around for decades.

The mayor said he had discussed the federal government’s immigration agenda with the president, as Immigration and Customs Enforcement has increased arrests in New York. Both men said they had discussed the Trump administration’s decision on canceling Temporary Protected Status for Haitians, which Mr. Mamdani has criticized as harmful to New Yorkers who currently have it and to the city’s economy overall.

“I shared my very strong opinions about how we can keep New Yorkers safe, how we can keep immigrant New Yorkers safe,” Mr. Mamdani said.

The number of ICE arrests jumped sharply in New York and across the United States this summer. The agency arrested 1,576 people in New York in July, the most in a single month since Mr. Trump returned to the presidency last year, according to the Deportation Data Project, an academic group. That is almost double the 869 arrests in April.

Mr. Trump acknowledged that Mr. Mamdani felt “very strongly” about the Temporary Protected Status issue but did not indicate that his administration planned to change course.

The mayor described the conversation as “productive,” adding that he had made the case to the president that the Haitian community was a critical part of the city’s economic backbone.

Donovan Richards, the Queens borough president, said he did not think the Sunnyside Yard project was the best way to increase housing affordability.

“I think there are so many other shovel-ready projects,” Mr. Richards said in an interview. “Queens is building. We are going through a renaissance moment.” Referring to the platform’s estimated cost, he added: “That $21 billion could be poured into the other projects in the queue.”

Mr. Mamdani defended his meeting with President Trump, his third in less than a year.

The mayor said he had a responsibility to speak with anyone who could help New Yorkers.

“I committed over the course of the campaign, as well as while being in office, that I would meet with anyone, work with anyone that could deliver for New Yorkers,” the mayor said.

“So having a conversation about potentially building 12,000 homes, about the importance of our Haitian neighbors here in this city, those are conversations that are worthwhile to have,” Mr. Mamdani added.

Presidential visits to the mansion are rare. Harry S. Truman attended a luncheon there in 1949 during ceremonies for the laying of the cornerstone for the U.N. headquarters. President John F. Kennedy delivered a major speech on medical care for older Americans there in 1962. And President Ronald Reagan visited in 1981 to promote the proposed Westway project on Manhattan’s West Side.

While Mr. Trump’s visit to Gracie was his first as president, he mentioned on Monday that it was not his first ever. In 2003, he attended former Mayor Rudolph W. Giuliani’s wedding there; in 2007, he and his wife, Melania Trump, attended a book party for Representative Charles Rangel held at the mansion. Michael R. Bloomberg, the mayor at the time, hosted both events.

This time, the president came at Mr. Mamdani’s request, in a continuation of their unlikely relationship.

The mayor and Mr. Trump insulted each other pointedly during the mayoral campaign last year, with the president labeling Mr. Mamdani a “lunatic” and Mr. Mamdani calling Mr. Trump a “despot.”

But at their first meeting, in the Oval Office in November, Mr. Trump, a New York native, praised the mayor-elect, saying he expected to help rather than hurt him. When a reporter asked Mr. Mamdani whether he still viewed the president as a fascist, Mr. Trump interjected: “You could just say yes. That’s easier.”

The two met most recently when Mr. Mamdani made an unannounced visit to the White House in February, after which the mayor said Mr. Trump had expressed support for the Sunnyside project.

Mihir Zaveri contributed reporting.