The Pentagon’s top general apologized on Thursday for appearing alongside President Trump near the White House after authorities forcibly removed peaceful protesters from the area, saying that it “was a mistake that I have learned from."

Gen. Mark A. Milley, the chairman of the Joint Chiefs of Staff, made the remarks in a prerecorded graduation speech to students at the National Defense University. He has been roundly criticized for thrusting the military into politics by walking alongside the president on June 1 as Trump traveled on foot to a nearby church that had been damaged during protests following the police killing of George Floyd.

Milley advised the students that it is important to keep “a keen sense of situational awareness” and that he had failed to do so as he walked from Lafayette Square in combat fatigues alongside the president, Defense Secretary Mark T. Esper and other senior advisers.

“As many of you saw the results of the photograph of me in Lafayette Square last week, that sparked a national debate about the role of the military in civil society,” Milley said. “I should not have been there. My presence in that moment, and in that environment, created the perception of the military involved in domestic politics.”

The apology follows a backlash from several retired senior officers, including Jim Mattis, who served as Trump’s first defense secretary.

In a scathing message, Mattis, a retired Marine Corps general, blasted the president for working to divide the country and took exception to the events in Lafayette Square. Mattis was motivated to write in part because he was appalled by the appearance of Milley in an event that critics said made it look as though Trump could use the military as a political club against opponents, several people close to Mattis told The Washington Post.

Mattis’s remarks marked an extraordinary shift, after he said repeatedly that he believed it was his duty to stay out of politics following his resignation as Pentagon chief in 2018. Many Democrats and other opponents of the president had criticized him for his stance, in light of his standing and knowledge of the president.

Senior defense officials, speaking on the condition of anonymity, had said Milley and Esper did not know what the president had planned at St. John’s Episcopal Church after walking through Lafayette Square. Milley did not appear in a group photograph in front of the church that Trump called for, but Esper did. The two leaders had worked that day to talk the president out of a much heavier response that included active-duty soldiers on America’s streets, the defense officials said.

Milley’s apology came at the end of a speech in which he decried the “senseless, brutal killing” of Floyd, a black man who died in custody after a white officer knelt on his neck for nearly nine minutes. The incident was captured on video and has prompted protests around the globe about police brutality and racism.

“His death amplified the pain, the frustration, the fear that so many of our fellow Americans live with day in and day out,” Milley said. “The protests that have ensued speak not only to his killing, but to the centuries of injustice toward African Americans."

Milley also called for military leaders to look for ways to improve equality, noting the military’s “mixed record” on the issue. While the military has come to reflect the diversity of the nation, he said, only 7 percent of generals and admirals are African American.

“We, too, have not come far enough,” he said. “We cannot afford to marginalize large portions of our potential talent pool, or alienate certain demographic groups."

Milley’s speech followed an announcement this week by state officials in Kentucky that another black man, David McAtee, was killed May 31 by the Kentucky National Guard at a barbecue restaurant he owned, thrusting the military further into the spotlight about its involvement in quelling national unrest.

The Louisville Metro Police Department have released surveillance footage that it says shows McAtee had fired a pistol as police cleared a parking lot outside his restaurant with pepper balls.

Guard members returned fire along with police after McAtee fired from the door of his shop, the Guard said in a statement. Brig. Gen. Hal Lamberton, the Kentucky National Guard’s senior officer, said in the statement that he believes an investigation “will conclude that it was a measured response from the National Guard that night.”

Milley did not mention the killing in Louisville. He said the National Guard has provided excellent support to law enforcement authorities as they respond to protests.

Alex Horton contributed to this report.