Quoting directly from the Smithsonian:
"On a Sunday morning in December 1941, a chaplain had his most difficult assignment — to say a prayer to sailors aboard a U.S. navy ship actively under low–flying attack by the enemy firing from all directions. He quickly realized the best he could do was walk the ammunition line saying, “Praise the Lord and Pass the Ammunition!” Stories of the overheard phrase quickly turned into legend and passed between soldiers, eventually reaching the press and one Broadway composer and lyricist, Frank Loesser. He served in the Radio Productions Unit charged with mounting shows of popular guest stars for boosting morale of the troops.
"With the phrase as the song title, he combined the stories into one set of lyrics — fact mattered not as much as the need to inspire spirit. By 1943, the song, performed by Kay Kaiser and His Orchestra, had reached no. 1 on the Billboard chart, surpassing its peak of no. 8 the previous year when performed by the Merry Macs. Loesser donated his royalties for sale of the song to the Navy Relief Society. The chaplain originally quoted wished to remain anonymous, and the episode to remain legend. Many variations on the tale, the name of the chaplain, whether or not he himself manned a gun turret to participate in the battle, and the battle's whereabouts have existed over the years."
SOURCE: https://www.si.edu/object/praise-lord-and-pass-ammunition%3Anmah_670902