I do not disagree with IG_42's answer and think it correct, but I would like to add to it a bit since I just watched this movie.
What the Movie Says
During the movie, All Might gives a partial explanation to what happened. Here is the quote:
Young Bakugo must've lost consciousness before the transfer finished. No, that's not it. For risking your life to protect. And for risking your life to prevail. All For One did something miraculous. Master and all of those who came before... thank you so much. I'm forever grateful.
In short, it was a miracle, just as IG_42's answer said.
There's also something akin to an explanation that Midoriya says. It doesn't really explain anything, but I'll include it anyway (Apologies, this one might not be the exact quote, but should be close enough).
Kacchan didn't remember about the transfer of All For One to him afterwards. And somehow the power remained with me.
So basically Midoriya's explanation is "I dunno."
Establishing it as a "miracle" is actually very convenient, because it means
- It does not need to be fully explained.
- We should not expect it to happen again. In other words, this is an anime movie where the writers are playing it loose with the rules of the established story, and we shouldn't expect the rules herein to carry over to the main story in the manga and anime.
So it was a miracle and needs no explanation. I'm going to explain it anyway (or try, at least).
First, we should examine the first thing All Might said. At first he suspects that Bakugo lost consciousness before the transfer finished. That's odd, actually. It tells us something about how the transference of All For One works. It means it doesn't simply complete immediately after the current holder transfers the power via an infusion of their DNA (at least according to All Might's understanding). Rather, the process of transferring from one user to the next continues even after that until it complete. It seems kind of strange to think of it like that, but that's what the story is saying.
Second, we should look at Bakugo's intentions. In his dialogue, he expressed concern that Midoriya would permanently lose his power. This shows that Bakugo probably did not intend to keep All For One, and would have given it back to Midoriya if he were given a choice.
Third, we look at the will of All For One. The vestiges of the past users living inside All For One would seem to have a will of their own. All Might even expresses in the quote that it was their intention to share the power with Bakugo, but to stay with Midoriya.
I suspect these three reasons are why Midoriya retained All For One. Namely, there was still a connection between him and Bakugo through All For One, and both Bakugo and the vestiges willed the power return to Midoriya.
As a side note, this isn't the first time something like this has happened in an anime movie. Go check out the first Broly movie in the Dragon Ball franchise, where they win the day by all giving their energy to Goku. Not sure how canonical being able to do that was at the time, but they did something like that when creating the Super Saiyan God, so I guess it's canonical now.
Further examples: https://tvtropes.org/pmwiki/pmwiki.php/Main/CombinedEnergyAttack and https://tvtropes.org/pmwiki/pmwiki.php/Main/EnergyDonation.