I'm not really sure. Perhaps when BZ was about to leave Obito's body to give the Rinnegan and Yin Kurama to Madara [
You must be registered for see links
], Obito took advantage of the moment and was able to regain control over his body/chakra. BZ probably was not expecting such resistance from an Obito on the verge of death and was caught off guard.
Also, even Madara was shown to be momentarily paralyzed when Obito punched his hand into Madara's body [
You must be registered for see links
] just as BZ later did. There's also the possibility that Kaguya's will on the outside (BZ) was able to resonate with her will/consciousness that was in the Shinju.