Two alleged Somali pirates previously accused of kidnapping an American journalist and holding him for more than two and a half years were hit with fresh federal charges on Wednesday — including new allegations they were working on behalf of terrorists.

Mohamed Thalil Mohamed and Abdi Hassad were previously hit with kidnapping raps in connection with the long captivity of Michael Scott Moore.

But defense attorneys for the pair were able to convince Brooklyn federal Judge Allyne Ross to drop the charges because the alleged crimes occurred out of US jurisdiction.

So prosecutors decided to take a second swing at the case — and are now arguing that Mohamed and Hassan nabbed Moore on behalf of a terrorist group.

The terror group was not named in court papers.

Dressed in jail garb, Mohamed and Hassad, both pleaded not guilty to the charges contained in a new indictment in court Wednesday.

“The charges that have been put forward are an overreach and will be dismissed,” James Kousouros, Hassad’s lawyer, told The Post following the arraignment.

Moore was researching a book about pirates on the Somali coast in January 2012 when a group of men holding assault rifles and rocket-propelled grenade launchers surrounded his vehicle.

The feds say defendants were part of a gang of outlaws who kept Moore in captivity. Hassad, whose lawyer has said held a high-level position within the Somali government, allegedly worked the phones with Moore’s relatives to shake them down for ransom money. Mohamed was allegedly charged with guarding and transporting Moore.

In May 2012, the crew released a video showing the journalist at a mystery location with a prayer shawl over his head, surrounded by masked kidnappers pointing a machine gun at him.

During his captivity, Moore, who wrote a book entitled “Desert and the Sea: 977 Days Captive on the Somali Pirate Coast” about his long ordeal, was pistol-whipped and suffered head injuries.

The gang of outlaws held him at several spots around the country for two and a half years, including on a hijacked fishing ship, but he finally found freedom after his family forked over $1.6 million for his release.

Moore’s publicist said he was not available for comment on Wednesday.