The five were all members of the D.O.M. Team 2008 group, which according to the police currently holds "fifth place for hacker attacks."
Two of the five are minors, and the other three are aged 19 and 20. They had communicated exclusively via the Internet and lived in four different Spanish cities - Barcelona, Malaga, Valencia and Burgos.
The police statement said that, "During the past two years the group hacked into 21,000 web pages."
The Spanish El Mundo newspaper had earlier reported that the group had hacked into the NASA website, but the police were unable to confirm this. They are also said to have hacked into the website of the Spanish telephone operator Jazztel.
The group would usually replace the websites with their own D.O.M. Team page.
The investigation began in March after the website of a leftwing Spanish political party, Izquierda Unida, was hacked into and filled with obscene messages following Spain's general election on March 9.
The five were released after questioning. Trial dates have yet to be set. The three older hackers face three years behind bars if convicted. They have claimed that they carried out their attacks in order to show up deficiencies in the defenses of those sites they hacked.