There are different versions of how Mameli actually came to write the anthem. One reports that Mameli took the anthem to the musician Michele Novaro a friend, who lived in Turin. Novaro composed the music, and Mameli returned to Genoa where he presented words and music to his friends. Shortly thereafter, "Fratelli D'Italia" (Brothers of Italy, another common name for the anthem) was played for the first time, at a popular assembly. The tune gained popularity throughout the peninsula, in defiance of the Austrian, Bourbon and Papal police.
The other and equally persuasive story goes that one evening in 1847, in the house of the American consul, the center of discussion was the uprisings of the day. Urged by many of the consul's guests, Mameli improvised a few lines on the spot and later wrote the rest. A few days later a friend took the poem to Turin and read it aloud at a nobleman's party. The composer Michele Novaro who was a guest at the same party, tried a few notes on the piano and then, went home to compose the song. The anthem was sung for the first time the next day by a group of political exiles in the Caffè della Lega Italiana of Turin. The anthem was chosen on a provisional basis in 1947 as the national anthem, however, the choice was never officially confirmed or re-examined, so officilaly, the anthem has been the "temporary" anthem of Italy for over 50 years!
There is talk lately of replacing the anthem, firstly because the music is not up to the standards of Italian classical music tradition (ironically, some of Italy's greatest composers have composed anthems in this tradition, which are used by Central and South American countries), and also the lyrics refer to specific events that were familiar to the Italians of the time that the anthem was composed, but bear little to no resonance with today's Italians. However, "Il Canto degli Italiani" is very recognizable in Italy and would be hard to replace.
Above is a stamp of anthem writer Mameli. Below is a postcard with the score of the Italian anthem.