When Saul became king of Israel, God's Spirit filled him: 1 Sam 10:6 "The Spirit of the LORD will come upon you in power, and you will prophesy with them; and you will be changed into a different person."
Saul was a great leader for twenty years: 1 Sam 14:47-48 "After Saul had assumed rule over Israel, he fought against their enemies on every side: Moab, the Ammonites, Edom, the kings of Zobah, and the Philistines. Wherever he turned, he inflicted punishment on them. He fought valiantly and defeated the Amalekites, delivering Israel from the hands of those who had plundered them."
But then, Saul began to disobey God. Melancholy and self-conceit started to take over his life and God's Spirit withdrew from him. It is then that: " . . . an evil spirit from God came forcefully upon Saul." 1 Sam 18:10. The ancient historian Josephus explained this as follows: "But as for Saul, some strange and demonical disorders came upon him, and brought upon him such suffocations as were ready to choke him" (Antiquities 6.8.2). The Bible explains clearly what this means in Ps 81:8-12:
"'Hear, O my people, and I will warn you-- if you would but listen to me, O Israel! You shall have no foreign god among you; you shall not bow down to an alien god. I am the LORD your God, who brought you up out of Egypt. Open wide your mouth and I will fill it.' But my people would not listen to me; Israel would not submit to me. So I gave them over to their stubborn hearts to follow their own devices."
In other words that evil spirit comes unto someone when that person rebels against God and decides to do his/her own will. God's grace is removed from that person as God will not impose Himself on anyone. This opens the doors to the devil's influence.
It looks as if God sends these evil spirits, but in reality, we make our own choice by turning our backs on God and following our own devices.