Tensions escalated between the two leading candidates for the Democratic nomination during a presidential forum on Monday, as polls show the race tightening in the final weeks before voting begins.

Hillary Clinton, the former secretary of state, and Vermont senator Bernie Sanders, who are locked into close primary races in Iowa and New Hampshire, sharpened their attacks on one another over gun control, tax policy and Wall Street reform at the Iowa Black and Brown Forum, hosted by Fusion at Drake University in Des Moines on Monday.

“It could be that the inevitable candidate for the Democratic nomination may not be so inevitable today,” Sanders told Jorge Ramos, a TV host on Univision and Fusion, when asked about Clinton’s recent attacks.

Democratic presidential candidate Senator Bernie Sanders at the Iowa Brown and Black Forum at Drake University. Photograph: Aaron P Bernstein/Reuters

Sanders also defended his 2005 vote for a law that shields gun manufacturers and sellers from lawsuits, which Clinton has continued to hold up as evidence that he is weak on gun control.

“It’s not a mistake,” Sanders said of his vote. “Like many issues of legislation, it’s complicated.” Sanders has said he would be willing to revisit the law.

On immigration, Clinton promised she would not be the next “deporter-in-chief” if elected. But she could not answerwhether she would deport children, saying the issue was more complicated than that. When the moderators pressed her for a more straightforward response, Clinton said only that children would receive “due process”.

During the forum, Clinton broke with President Barack Obama and called on his administration to stop the recent deportation raids that have targeted Central American migrants who crossed the border in mass last summer fleeing violence and poverty in their native countries.

“Our immigration enforcement efforts should be humane and conducted in accordance with due process, and that is why I believe we must stop the raids happening in immigrant communities,” Clinton said in a statement that her campaign released during the forum. Senator Sanders and Governor Martin O’Malley had already condemned the raids and urged the president to end them.

Clinton also told the moderators that she did not see a “contradiction” between her support for immigration reform and her calls for increased border security, which she said included funding for a fence not a wall. Sanders’ campaign has already seized on the comment as a potential line of attack.

The race for the Democratic nomination has become more contentious as the clock winds down to the Iowa caucus, now less than three weeks away.

Clinton, who until recently held a comfortable lead in Iowa, has ratcheted up her attacks on the Vermont senator’s record on guns, and earlier on Monday opened up a new line of attack on the Vermont senator over his health care policy.

An NBC poll of Iowa voters released on Sunday found that Sanders trailed Clinton by just three percentage points in a state with a large swath of swing voters and a tradition for supporting anti-establishment candidates.

The forum was designed to focus on African American and Latino communities in the US, allowing the candidates to respond separately to questions raised by journalists and the audience on a range of issues, including immigration, education and criminal justice.

An Iowa Brown & Black Presidential Forum for Republican candidates was cancelled because of “unresolved scheduling conflicts”. It would have been the first time Republican presidential candidates participated in the forum.

The fast-paced format drew frank responses from candidates on a wide range of issues, major policy points such as immigration and criminal justice and specific questions about reparations and white privilege.

A college student asked Clinton to define white privilege and explain how she had benefitted. “Where do I begin?” she said.

Sanders said the African American community was “absolutely” justified in its distrust of law enforcement, noting that police killings have been happening for many decades and the difference now was that the encounters were being captured on camera.

Yet the presidential forum drew as many laughs as contrasts. A handful of playful, rapid-fire questions inspired some notable one-liners and, of course, a few shots at Donald Trump.

When asked if it was “off-brand” for a democratic socialist to live in a mansion like the White House, Sanders replied: “I would consider it more like public housing.”

Governor Martin O’Malley, who is struggling to gain traction in the polls, was asked: “So you think you can be president?” “Yes, I do,” he replied, later acknowledging that he was the only candidate in the Democratic race who could “still upset the apple cart”. But O’Malley blamed the Democratic National Committee for his flagging presidential campaign, accusing it of designing an “undemocratic” debate schedule.

Clinton admitted that she didn’t get Donald Trump anything for his wedding in Florida. “He was basically a Democrat before he was a Republican,” she replied.

Clinton, who was the last candidate to be interviewed on stage, received the final question of the night: “Can you say categorically tonight that Bernie Sanders cannot win the presidency?” Ramos asked her.

“Anybody can win,” Clinton said, laughing. “Who would have thought Donald Trump would be leading in national polls?”