Necessary? Well, maybe not absolutely necessary …depending on what you want to write.
A three chord song? …Using I, IV, and V chords? …Probably not required, but then it’s more just copying others and putting your own words to it.
If you want to write, you first learn theory and the method of writing called counterpoint. It won’t tell you what to write, but points you in the right direction regarding harmony etc.
And like anything, your skill improves with practice and further learning. It’s faster with help from an instructor.
Without instruction, you have to experiment to find out what works. It’s easier if you learn from examining the works of other composers. …Easier if you learn the ‘rules’ by which they composed.
Then there are all the aspects of notation to consider. …Just how to notate what you want them to play. …You want what you write to be something others can read.