Moments before, authorities had used pepper spray and flash bangs to clear the park and streets of largely peaceful protesters rallying over the death of George Floyd.
“I should not have been there,” General Milley, the chairman of the Joint Chiefs of Staff, said in remarks to a National Defense University commencement ceremony.
“My presence in that moment and in that environment created a perception of the military involved in domestic politics,” he added.
“As a commissioned uniformed officer, it was a mistake that I have learned from, and I sincerely hope we all can learn from it.”
General Milley said his presence and the photographs compromised his commitment to a military divorced from politics.
US Attorney General William Barr and US Secretary of Defence Mark Esper also walked with Mr Trump, but General Milley’s presence was particularly criticised as he was wearing his camouflage battle uniform.
Normally military officials wear their formal dress uniform when holding meetings in the White House – but for many it implied his support for Mr Trump’s wish to deploy active duty US troops against protesters.
Mr Esper has not said it was a mistake to appear with Mr Trump.
He told a news conference last week that when they left the White House, he thought they were going to inspect damage in the Square and at the church and to mingle with National Guard troops in the area.
General Milley’s public expression of regret – his first public statements about the event – now risks the wrath of Mr Trump.
It also comes as Pentagon leaders’ relations with the White House are still tense after a disagreement over the president’s threat to use federal troops to quell civil unrest triggered by Floyd’s death.
When Mr Esper told reporters on June 3 that he had opposed Mr Trump bringing active-duty troops on the streets of the nation’s capital to confront protesters and potential looters, Mr Trump castigated him in a face-to-face meeting.
Speaking at the commencement ceremony, General Milley said all senior military leaders must be aware that their words and actions will be closely watched.
He said the lesson to be taken from his mistake was that all in uniform are not just soldiers but also citizens.
“We must hold dear the principle of an apolitical military that is so deeply rooted in the very essence of our republic,” he said.
“It takes time and work and effort, but it may be the most important thing each and every one of us does every single day.”
He also expressed his outrage at the Floyd killing.
“What we are seeing is the long shadow of our original sin in Jamestown 401 years ago,” he said, referring to the year in which the first enslaved Africans arrived on the shores of colonial Virginia.