The use of "gun" as a noun to refer to all the children in the school serves three linked purposes.
Initially it purposefully confuses the reader as to what's happening in the poem, while ensuring that the focus is on guns. The opening of
the gun heard the first shot
Is a deliberate obfuscation. How can a gun hear itself? It isn't until we get to
the gun’s teacher told everyone to get on the ground
That's there's any clarity as to what this poetic device is doing, and it may be a couple more lines before it's completely clear to the reader. By doing this, the reader's confusion mimics the confusion of a child in the initial stages of the shooting while ensuring the focus of both their thoughts is on guns.
This latter effect is the second and central thrust of this device. Through the entire poem the reader cannot forget, even for a moment, that a gun is the centerpiece of this event, just as those trapped in a shooting cannot forget that either.
Finally, it also helps to bring home the political subtext of the poem about the danger of guns and the need for gun control, demonstrated by the later references to "thoughts and prayers" and "second amendment". Through the repetition of the word "gun' the reader is not allowed to forget that without the presence of the gun, this terrifying tragedy would not be happening.
The poet has used this device because it is simple and highly effective. By using "gun" as the noun in place of "child" or a gendered alternative the poet can get this effect of repetition without burdening the poem with clumsy language to mention a gun in every line, diluting its direct impact. It also makes it easy to keep the poem in the first person, heightening the reader's empathy with the child experiencing this event.
This is an extreme example of a poetic technique called anaphora. Merriam-Webster defines it as:
repetition of a word or expression at the beginning of successive phrases, clauses, sentences, or verses especially for rhetorical or poetic effect