I was trying to remember this book title too! I think it's The Eye of Minds by James Dashner. The summary from Goodreads:
Michael is a gamer. And like most gamers, he almost spends more time on the VirtNet than in the actual world. The VirtNet offers total mind and body immersion, and it’s addictive. Thanks to technology, anyone with enough money can experience fantasy worlds, risk their life without the chance of death, or just hang around with Virt-friends. And the more hacking skills you have, the more fun. Why bother following the rules when most of them are dumb, anyway?
But some rules were made for a reason. Some technology is too dangerous to fool with. And recent reports claim that one gamer is going beyond what any gamer has done before: he’s holding players hostage inside the VirtNet. The effects are horrific—the hostages have all been declared brain-dead. Yet the gamer’s motives are a mystery.
The government knows that to catch a hacker, you need a hacker.
And they’ve been watching Michael. They want him on their team.
But the risk is enormous. If he accepts their challenge, Michael will need to go off the VirtNet grid. There are back alleys and corners in the system human eyes have never seen and predators he can’t even fathom—and there’s the possibility that the line between game and reality will be blurred forever.
And the twist you mention about him being an NPC from the Wikipedia synopsis:
.... Michael then wakes up in a Coffin, or a coffin-like enclosure from which the VirtNet is accessed, but realizes that his body and his surroundings are different. He finds that Kaine left him a message that explained how Michael was a Tangent, and that he was the first successful subject of The Mortality Doctrine, which implants Tangent intelligence into human bodies. Michael is also told that since he is now human, his headaches were actually caused by Decay, a condition that results from the deterioration of a Tangent's code. Michael then realizes that he had resided in the game Lifeblood Deep during his time as a Tangent, and when he had entered his Coffin, he had entered the game used by human beings, Lifeblood. He opens the door and meets Agent Weber, the VNS agent who contracted him to stop Kaine, who informs him that Bryson and Sarah are real. He is also told to attempt to impersonate the human whose body he is in.