A ferry ride to Puerto Rico turned into a rescue at sea Wednesday when a fire broke out in the engine room and quickly started spreading through the ship.

While the crew of the Caribbean Fantasy battled the blaze, the U.S. Coast Guard moved in and safely evacuated the 512 passengers — and seven pets — on board to San Juan harbor, officials said.

Two dozen passengers were treated at local hospitals for smoke inhalation, although four suffered broken bones in the rush to escape the blaze, they said. At least four of injured were children between the ages of five and six.,

Meanwhile, the stricken ship was towed to the Isla de Cabra where investigators were trying to figure out what sparked the fired.

The ship, which has a crew of 26, transports mostly Dominican passengers back and forth from San Juan to the Dominican Republic.

It was about an hour out of San Juan when the fire broke out at 7:15 a.m., Coast Guard spokesman Ricardo Castrodad said.

Gyno Funes said he was one of two mechanics in the control room when a hose carrying fuel burst open and caught on fire.

"We were trying to extinguish it for two hours, but couldn't," said the other mechanic, Marlon Doblado, after the two reached shore.

During the evacuation, several dozen passengers were carried in on stretchers and later treated for dehydration, high blood pressure and respiratory problems due to the smoke, fire department spokesman Juan Vega said.

One stretcher held a man hooked up to an IV who was cradling a bawling newborn baby clad only in a diaper, witnesses said.

The injured were followed by a small group of children who held hands as they boarded the Coast Guard boats.

Among the travelers were dozens of school-age Dominican athletes headed to competitions in Puerto Rico, including a 22-member cycling team, a girls' volleyball team and a boys' baseball team, the Associated Press reported.

The passengers who did not require medical attention were loaded onto city buses and driven from the port area. They included Fiordalise Pitchard, who traveled to Puerto Rico on vacation.

"I feel nervous," she said, her hands trembling. "I was in the restaurant when I saw the smoke. I'm giving all the glory to the Lord today."

Anguished relatives of passengers, including Hilda Pereira, pushed their way onto the scene. Her daughter, whom she has not seen for 14 years, was aboard the boat.

Pereira said she was at the beauty salon when her sister called to notify her.

"She said, 'You need to be strong,'" Pereira said. "The ferry has caught on fire."