A Royal Saudi Air Force cadet who opened fire last month at a Florida naval base and killed three American service members in an act of terrorism marked last year’s anniversary of the Sept. 11 attacks by declaring on social media “the countdown has begun.”

Over the Thanksgiving weekend, he visited the 9/11 Memorial in New York City.

Those moves contributed to the Justice Department’s confirmation Monday that the shooting at Naval Air Station Pensacola was a terrorist attack.

“The evidence shows the shooter was motivated by jihadist ideology,” Attorney General William P. Barr said in the announcement.

Mohammed Saeed Alshamrani, 21, regularly posted anti-American, anti-Israeli and jihadi messages on social media. He did so just before his attack the morning of Dec. 6, when he “proceeded to walk around shooting down unarmed victims in cold blood,” Mr. Barr said.

A sheriff’s deputy responding to the scene fatally shot Alshamrani about 15 minutes into his rampage.

FBI Deputy Director David Bowdich said Alshamrani fired shots at pictures of President Trump and a former U.S. president, and witnesses said he made statements criticizing U.S. military action overseas.

Investigators said Alshamrani did not appear to be inspired by a specific terrorist group but instead harbored anti-U.S. and anti-Israeli sentiments.

Mr. Bowdich said the shooter believed “violence was necessary.”

The attack raised concerns about how well foreign military students are screened. Mr. Barr and other Justice Department officials did not explain how Alshamrani’s social media activity was missed during the vetting process to train at a U.S. military base.

“I think we’ve identified that the vetting should be improved for these candidates, but I don’t want to suggest or speculate improved vetting would have prevented this event,” Mr. Barr told reporters.

Alshamrani is believed to have had a secret Twitter account that did not include a name, photo or biographical information, making tweets harder to link to him.

Mr. Barr also debunked reports that the shooter was accompanied by Saudi cadets who fled the attack and said there was no evidence that other members of the Saudi military knew about or assisted Alshamrani before or during the attack.

The Saudi cadets happened to be in the area and took videos of the chaos, said Mr. Barr, noting that the cadets cooperated with investigators.

Mr. Bowdich said their conclusions were based on interviews with more than 500 people, including witnesses, base staff and the shooter’s friends and classmates.

However, the investigation uncovered evidence that 17 Saudi servicemen training at U.S. military bases shared jihadi or anti-American propaganda on social media. There was no indication that any of them was affiliated with terrorist activity or a group.

Fifteen, including some of those who spread the jihadi material, were found to have possessed child pornography, Mr. Barr said. Only one was found to have “a significant number” of images, the attorney general said.

U.S. attorneys reviewed each case and decided that the evidence collected was not enough to trigger a federal prosecution, Mr. Barr said.

Twenty-one cadets from Saudi Arabia have been removed from training at U.S. bases and will be returned to their home country.

Roughly 852 Saudi nationals are training with the U.S. military under a cooperation agreement. The U.S. began scrutinizing the Saudi soldiers in the wake of the attack.

Saudi Arabia has decided that the 21 cadets engaged in conduct unbecoming of an officer of the Saudi Royal Air Force and Royal Navy, Mr. Barr said. The kingdom has promised to review each case under its military justice codes and criminal law.

During the gunfight with first responders, Alshamrani disengaged long enough to place one of his two Apple iPhones on the floor and shoot a single round into the device, Mr. Barr said. He also damaged the other phone.

Law enforcement is working to unlock the two phones. Mr. Barr said the FBI has asked Apple for help, but the tech giant has not provided any “substantial assistance.”

“It is very important to know with whom and about what the shooter was communicating before he died,” he said.

He declined to comment on whether the government would go to court to force Apple to cooperate. Courts have not ruled on whether the government can force technology companies to give them access to smartphones and other personal devices.

Apple said in a statement that it helped FBI agents by sharing relevant data stored in the phone’s cloud.