“We may have our differences, but we do well in times like these to remember that everyone who serves in our nation’s capital is here because, above all, they love our country,” Mr. Trump said.

It was a sadly familiar ritual- an American president addressing the nation at an unsettling time, decrying violence while urging citizens to set aside their differences and pray for the recovery of victims.

But this time, it was President Donald Trump who was called upon to speak words of comfort in such a troubled moment, one fraught with the overtones of gun politics and the heated rhetoric of a nation sharply divided along party lines.

Mr. Trump’s measured response to Wednesday’s shooting at a congressional baseball practice stood in stark contrast to his inflammatory reactions to some previous acts of violence. He delivered a brief address from the White House Diplomatic Room in which he denounced the shooting of a top House Republican and others as a “very, very brutal assault.” He said that “many lives would have been lost without the heroic action” of Capitol Police officers who took down the gunman.

“We may have our differences, but we do well in times like these to remember that everyone who serves in our nation’s capital is here because, above all, they love our country,” Mr. Trump said. “We can all agree that we are blessed to be Americans, that our children deserve to grow up in a nation of safety and peace and that we are strongest when we are unified and when we work together for the common good.”

Republican Rep. Steve Scalise of Louisiana was shot in the early morning fusillade of gunfire, and several other people, including members of Scalise’s security detail, also were wounded. The gunman was killed.

On Wednesday evening, the President and first lady Melania Trump visited MedStar Washington Hospital Center, where Mr. Scalise remains in critical condition.

“The President entered the room, spoke with Scalise’s family members and sat by his bedside with Mrs. Trump,” Press Secretary Sean Spicer said, describing the scene in the intensive care unit as emotional. Mr. Scalise and his wife, Jennifer, have two children.

The President was briefed by Scalise’s medical team and also visited with Capitol Police officer Crystal Griner, who was shot in the ankle during the attack.

Back at the White House, Mr. Trump tweeted- “Rep. Steve Scalise, one of the truly great people, is in very tough shape — but he is a real fighter. Pray for Steve!”

Mr. Trump was informed of the shooting minutes after it occurred. The White House press office quickly put out a brief statement noting that Mr. Trump was “deeply saddened by the tragedy,” and the President followed up with a tweet- “Rep. Steve Scalise of Louisiana, a true friend and patriot, was badly injured but will fully recover. Our thoughts and prayers are with him.”

Wanting to present the President as a steadying hand and avoid any distractions after the shooting, the White House then quickly canceled a presidential event on apprenticeships at the Labor Department and scuttled any plans for a briefing. Instead, aides drafted the short, somber remarks Mr. Trump delivered from the Diplomatic Room.

The President praised Mr. Scalise as “a very good friend” and “a patriot,” telling the legislator he had “not just the prayers of the entire city behind you, but the entire nation and frankly the entire world.” He praised Capitol Police officers and first responders who mobilized at the softball field where the Republican baseball team was practicing ahead of Thursday night’s charity game against the Democrats.

“Their sacrifice makes democracy possible,” he said.

Mr. Trump also broke the news that the shooter, 66-year-old James T. Hodgkinson of Illinois, had died. Hodgkinson had a history of lashing out at Republicans and apparently had volunteered for Bernie Sanders’ presidential campaign.

Several prominent Republicans, including the President’s eldest son, were quick to link the gunfire to anti-Trump rhetoric from the left. But in the hours after the shooting, the President, whose pugnacious style has come to define this era of bruising partisanship, avoided any mention of the political debate surrounding the shooting.

Mr. Trump showed little of that restraint when reacting to acts of violence during his campaign. He drew sharp criticism a year ago when he tweeted “Appreciate the congrats for being right on radical Islamic terrorism” in the wake of the shooting at a gay nightclub in Orlando, Florida, that left 49 people dead. He also was accused of inciting violence when he warned that the “Second Amendment people” among his supporters might find a way to stop his opponent, Hillary Clinton, from rolling back gun rights.

Earlier this month he used Twitter to denounce the mayor of London in the wake of a terror attack that left seven dead there.

Mr. Trump’s brief speech at the White House was reminiscent of the more than a dozen times that his predecessor had to address the nation after a mass shooting. Some of the most indelible images of Barack Obama’s presidency followed an act of violence, including the tears in his eyes while mourning the 26 people, including children, killed in Newtown, Connecticut, in 2012 and his rendition of “Amazing Grace” while delivering a eulogy after nine churchgoers were killed in Charlestown, South Carolina, three years later.

While Mr. Obama’s statements of mourning were frequently paired with a plea for stricter gun control laws, Mr. Trump did not mention firearms regulations.