Thanks for your question. I just taught on 1 Samuel this last Sunday.
Several years ago I taught this sermon on that text.
God is good. That does not mean that God does not allow suffering (see Psalm 73)
God is righteous, and that means that God cannot overlook sin. Saul had sinned, and he had rebelled against God. The Spirit was given to Saul so that he could reign as king. Saul was rejected as Israel's king, and David was anointed by Samuel. The Spirit of God came upon David, at the same time it left Saul (1 Samuel 16:13-14). An evil spirit then came upon Saul. This spirit could not afflict Saul without God's permission, so that ultimately this spirit came from God.
Saul's "evil spirit" is something like Satan coming upon Judas, to betray the Lord's anointed (John 13:2, 27). God's Spirit did not leave Saul, nor did the evil spirit enter him until after Saul had rebelled against God and God had announced to him that his kingdom was to go to another (1 Samuel 15:28).