I'm looking for a book/series that I read probably 8-10 years ago (although it could have come out earlier). In the book, the hero makes some kind of deal with a possessing entity, where that entity gets control of their body for a couple minutes, but the deal involves the entity not doing anything bad (like hurting or killing their friends, etc.). At a climactic moment, the entity takes control of the hero to stop them doing something important/stopping something bad from happening.
Other things: I'm pretty sure it was a high-fantasy children's book, in the vein of Fablehaven.
Edit: Some other details I can dredge up: the hero in question is a male child, and I think it's a book where the fantasy world is separated by a portal/gate from Earth.