Civil Guard boats stopped the vessel off Melilla, a Spanish enclave in northern Morocco, Spanish government officials said in a statement.
Two of the migrants jumped into the water while three others confronted Spanish officers, "one with an anchor, one with a club and a third by throwing petrol at the guards," it said.
"The violent reaction of some of the occupants of the boat once it was intercepted by the Civil Guard obliged the officers to detain three of them for resisting and assault against the guards," it said.
The person in charge of the vessel was also detained and will be charged with migration-related offences, the statement said. The four detainees will later go before a judge.
The other 13 migrants were taken to Melilla, the statement added. The Red Cross attended to them there, providing them with blankets, but none of the migrants were injured, a source in the organisation told news agency AFP.
The government said two officers were slightly injured in the skirmish.
It did not specify the nationalities of the migrants but said they were all "sub-Saharan" Africans, including two pregnant women and two children.
They were the latest in a flow of migrants trying to reach Melilla and the other Spanish north African territory of Ceuta to the west, which has intensified this year.
The Civil Guard has drawn criticism from rights groups this year for violent incidents at the fenced-off frontiers of Melilla and Ceuta, which have Europe's only land borders with Africa.
Spanish authorities blame human-traffickers for bringing the migrants to the border in the hope of reaching European soil.