The Wikipedia article tells how in 1818 Franz Gruber was asked by Father Joseph Mohr to compose a melody and guitar accompaniment for that night's mass when river flooding took the church organ out of service.
https://en.wikipedia.org/wiki/Silent_Night