Obito explained that while we was the Ten-Tails Jinchuriki, he could not use Kamui, his Mangekyo Sharingan's unique ability. He was later able to use it when the Ten-Tails was extracted from him, and while we was trying to take the Tailed Beasts from Madara and while fighting Kaguya Otsutsuki.
The same is explained in the Naruto wiki:
There is really something wrong on how Kamui interacts with Juubi's Jinchuriki body. Way back in Chapter 643, it was noted that by being the ten-tails jinchuriki, Obito can no longer use his Kamui to travel through his pocket dimension nor to become intangible.
(emphasis my own)
But while Madara was on his goal to retrieve his other Rinnegan which Obito hid, he was able to use Kamui to get to Obito's pocket dimension and take his Rinnegan back. But he did all of this while he was the Jinchuriki of Ten-Tails.
It's shown in this YouTube video that Madara uses Kamui to retrieve his Rinnegan.
To sum it up, Obito, the original owner of the Kamui ability was not able to use the ability while he was Ten-Tails Jinchuriki. However, while Madara was Ten-Tails Jinchuriki, he used Kamui, retrieved his Rinnegan, then came back to the actual world.
Why did this happen? Is it ever explained?