The Spanish language along with Italian, French, Portuguese and Romanian have origins in the beautiful Latin. All these languages have in common the phoneme [ɲ]
Nevertheless, the representation of this phoneme is different for each language: Gn in Italian and French, ny in Catalan, nh in Portuguese.
In the beginning of the XII century, the Spaniards started putting an accent on top of the letter (which name is “virgulilla de la ñ”) because they needed to have a graphical representation for this sound.
Nowadays, the ñ is a unique symbol of identity for one of the most interesting and popular languages.