During a visit to Spain early Sunday morning, President Obama defended the Black Lives Matter movement and the right of activists to demonstrate in cities across the United States, following a long night of ongoing protests over the highly public deaths last week of two black men at the hands of police.

"One of the great things about America is that individual citizens and groups of citizens can petition their government, can protest, can speak truth to power," Mr. Obama told reporters in Madrid. "And that is sometimes messy and controversial but because of that ability to protest and engage in free speech, America over time has gotten better. We've all benefited from that."

The president pointed to sometimes contentious demonstrations in American history -- spawned by abolitionists, suffragettes, civil rights activists, and others -- that faced criticisms similar to the Black Lives Matter movement.

Get Breaking News Delivered to Your Inbox

"There were times when activists engaged in rhetoric that was overheated and occasionally counterproductive," Mr. Obama conceded, "but the point was to raise issues... I think what you're seeing now is part of that long standing tradition."

"I don't think that you can hold well-meaning activists who are doing the right thing and peacefully protesting responsible for everything that is uttered at a protest site," he continued. "This week people felt hurt and angry and so some of this is just venting, but I think that the overwhelming majority of people who are involved in the Black Lives Matter movement -- what they really want to see is a better relationship between the police and the community so they can feel that it's serving them."

In the wake of the violent slayings of police officers in Dallas, however, the president also cautioned that if any BLM sympathizers attack law enforcement -- both in their rhetoric and in their actions -- they are "doing a disservice to the cause."

"Any violence to police officers is a reprehensible crime and needs to be prosecuted," Mr. Obama said. "But even rhetorically, if we paint police in broad brush without recognizing that the vast majority of police officers are doing a really good job and are trying to protect people and do so fairly and without racial bias, if our rhetoric does not recognize that, then we're going to lose allies in the reform cause."

"Maintaining a truthful and serious and respectful tone is going to help mobilize American society to bring about real change -- and that is our ultimate objective," he said.

While in Madrid, the president declined once more to comment on the recently closed Justice Department investigation into the private email server use of Hillary Clinton, the Democrats' presumptive White House nominee.

He lauded FBI Director James Comey for taking "the extraordinary step of explaining in methodical fashion how they arrived at their conclusion" not to prosecute Clinton and noted that it was "inappropriate" to second guess or comment extensively about the matter.

The president is expected to cut his trip to Europe short after the unrest in Dallas. He will return to Washington, D.C. Sunday evening before heading to Dallas at the invitation of Mayor Mike Rawlings on Tuesday. Mr. Obama will deliver remarks at an interfaith memorial service while in Texas.