The captain who ran an NYC Ferry boat onto a sandbar off the Rockaways — stranding nearly two dozen passengers for nearly six hours — is suspected of causing the ordeal by straying off course, the company that runs the maritime service said Thursday.

“While preliminary information indicates the vessel was off route, that’s something investigators will look at during the investigation,” Hornblower spokeswoman Skye Ostreicher said in a prepared statement.

Ostreicher added: “Safety is our number one priority, and we will continue to review our training programs in order to ensure rider and crew safety.”

The captain who was ​at the helm Wednesday wasn’t identified, but ​”has been taken off duty pending the outcome of the investigation​,” said a spokesman for the city Economic Development Corp., which oversees the NYC Ferry fleet.​

Co-workers told The Post the captain hadn’t piloted an NYC Ferry boat for some time and may have been unfamiliar with the waters where he ran aground.

The captain was brought in to replace the route’s regular captain, an experienced mariner known as “Danny” who called out ahead of his Wednesday shift, one worker said.

“Danny’s really good. He wasn’t driving. He usually drives,” the worker added.

The ferry is being inspected for damage and will be repaired, if necessary, EDC spokesman Anthony Hogrebe said.

It has to be certified by the US Coast Guard before being put back into service, he added.

The ferry, named the Flyer, was headed to Wall Street’s Pier 11 when it got stuck in the Coney Island Channel, northwest of Breezy Point Tip park, around 5:30 p.m.

Rescue efforts were hampered because the water in the area was too shallow for other boats to approach, the Coast Guard said.

The 23 passengers and four crew members eventually were shuttled on inflatable NYPD dinghies from the stranded ferry to an FDNY vessel and brought to a marine terminal in Brooklyn.

Temperatures in the area during the rescue were in the mid-teens, with wind-chill readings in the single digits.

“The only time you froze was when they would put you on the police boat,” passenger Jeffrey Spears said afterward.

“It felt like the raft would never come fast enough and even the raft got stuck. They had to use a stick to keep it from getting stuck.”

Hornblower is trying to make amends by giving each of the passengers a yearly ferry pass, a $50 Uber credit and a dinner-and-movie package at the iPic Theater at the South Street Seaport.

Several Rangers fans who were headed to Madison Square Garden to watch the Blueshirts take on the Washington Capitals — whom they beat 1-0 — will also be reimbursed for the cost of their tickets and receive reservations for a future game, the company said.