The story of King Josiah has defied simple explanation. Many readers have asked the obvious question, Why does God not save the life of the best King and worshiper, 'since David'? Why does the prophetess Hulda promise a peaceful death but the story rather has him die in battle? Why does the following section of 2 Kings (23:31-37), Jeremiah and Zephaniah not seem unaware of any of the reforms Josiah was said to have done, but rather describe him as one of the kings who had 'done evil in the eyes of the Lord'?
The answer is the composite nature of the work. Russell Gmirkin did an excellent piece on the redactions of 2 Kings. (3) The Manasseh and Josiah Redactions of 2 Kings 21-25 | Russell Gmirkin - Academia.edu
You can read it with a free registration. I strongly encourage anyone to read his thesis carefully as he also does an excellent job describing the intertextual nature of the books. It is exhaustive and lengthy but good arguments often are.
In short, the convincing proposal is that the story of Josiah as it reads now was a compilation of the story of the decline of the Kingdom and eventual punishment with an incongruent tale of Josiah reforms.
As Gmirkin recreates the original storyline:
...the sins of Manasseh as the reason for the downfall of the Jewish nation and both include the final kings of Judah....All his descendants on the throne continued in his sins down to the fall of Jerusalem to Nebuchadnezzar. Under one of his descendants, Josiah, a copy of the law discovered in the temple foretold the disasters that would befall the Jews, predictions reaffirmed by the prophetess Huldah...Josiah suffers an ignominious death... Yet the kings and the nation never turned from their evil ways, and so the temple was destroyed, and the Jews went into captivity, including the royal line.
2 Kgs 22.12-17 was followed by the passage on the sins of Manasseh at 2 Kgs 23.26-27 and the account of Josiah’s ignominious death at 2 Kgs 23.28-30
Gmirkin suggests the primary storyline and the alternate story of Josiah's reforms were written nearly contemporaneously by separate authors then not long after clumsily combined. He also dates this process in the Greek era, though the arguments here do not necessary require that.
This Josiah reform story in 2 Kings can be understood as an alternative history written without familiarity of the negative treatment of Josiah in the primary version of 2 Kings. It basically serves as a doublet of sorts of the story of Hezekiah who likewise makes extensive religious reforms at the prodding of a prophet. (Jer. 26.18-19)
There are also literary links between those parallel stories, including the statements said of both. Of Hezekiah:
"There was no one like him among all the kings of Judah, either before him or after him.
Of Josiah:25 Neither before nor after Josiah was there a king like him who turned to the Lord as he did—with all his heart and with all his soul and with all his strength, in accordance with all the Law of Moses.
Obviously, the writer of the Hezekiah description is unaware of the Josiah reform story that follows it in the present text.
In my opinion, the 2 writers had inherited a tradition of a reformer but differed as to the name of the King. The compiler found it easiest to simply include both versions without a great deal of concern for consistency.