Men, women, children and two babies, many of them wearing life jackets, were massed onto the 20-metre (66-foot) long vessel - some perched on the edge - in an attempt to cross the Mediterranean and reach Europe.
The rescue operation lasted more than six hours, the ministry said, as crew from the frigate Canarias ferried refugees to their ship on board small inflatable, motorised boats.
"The Canarias frigate is headed to the Italian port of Lampedusa where the 517 immigrants on board will disembark," it added.
The rescue was part of the EU's Operation Sophia, which aims to arrest smugglers and seize their vessels in a bid to stem the flow of migrants making the perilous sea crossing to Europe.
This year alone, more than 3,000 people have died in the crossing, while more 700,000 have made the voyage, mostly landing in Greece and Italy, according to the International Organization for Migration.
Operation Sophia - named after a baby girl who was born on a German vessel after her Somalian parents were rescued off Libya -- is manned by hundreds of personnel from 22 European countries.