The promise which the Lord made to Josiah in 2 Kings 22:20 was because of the splendid work of reformation. King Josiah had inaugurated and was prosecuting against the false and idolatrous religious systems in the land. While it is not specifically stated that this promise was conditioned on Josiah's continued faith and obedience to the Lord, yet we can be assured that was the case. All of God's promises are fulfilled based on faithfulness. Since Josiah did not die in peace, but was slain in battle, we look for where Josiah was disobedient. The incidents narrated in the next chapter, are described more in detail in 2 Chronicles 35. The reason why Josiah suffered a violent death is stated in verse 20. What a lesson for all–to mind their own business and not to meddle in the affairs of others!
2 Chronicles 35:20-22 (NIV), "20 After all this, when Josiah had set the temple in order, Necho king of Egypt went up to fight at Carchemish on the Euphrates, and Josiah marched out to meet him in battle. 21 But Necho sent messengers to him, saying, 'What quarrel is there, king of Judah, between you and me? It is not you I am attacking at this time, but the house with which I am at war. God has told me to hurry; so stop opposing God, who is with me, or he will destroy you.' 22 Josiah, however, would not turn away from him, but disguised himself to engage him in battle. He would not listen to what Necho had said at God's command but went to fight him on the plain of Megiddo."