Here is the Haydock Bible Commentary’s take on it
Ver. 14. From the Lord. An evil spirit, by divine permission, and for his punishment, either possessed or obsessed him. (Challoner) — We no longer behold in Saul any generous sentiments. He falls a prey to melancholy, anger, suspicion, and cruelty. “He was seized with an illness, inflicted by the devil, says Josephus, ([Antiquities?] vi. 9.) so that he seemed to be choking; nor could the physicians discover any other means to alleviating his distress, except by employing some person skilled in music… David alone could bring the king ot his right senses, by singing hymns with the sound of the harp. Wherefore Jesse consented that his son should remain with the king, since he was so much delighted with his company.” (Haydock) — The Jews, and many Christians, suppose that Saul’s illness was melancholy, or “madness,” as St. Chrysostom calls it. It was inflicted by an evil, or even by a good angel, as the minister of God’s vengeance, (Exodus xi. 4.; Calmet) who punished his former pride and rebellion, by reducing him to so mean a condition. (Haydock) — St. Augustine and Ven. Bede suppose, that the evil spirit troubled him by God’s permission. (Worthington)
The way I see it is it was a way of repenting a way to bring Saul back to God and to dismiss all of his faults to that point.