The attack is wasted if there are no other creatures nearby, assuming the saving throw against sanctuary is failed.
If you're attacked first, then sanctuary activates. Assume the creature fails its save, because if it passes, the question is moot. The only available target is your mount, which it then chooses to attack. Mounted Combatant can then be used to force the attack to target you again. If the creature fails again, the attack is wasted, as the attack is forced to target you instead of your mount.
If your mount is attacked first, you can use Mounted Combatant to force the attack to target you instead. Sanctuary then activates, and the creature fails. It's unable to attack the mount again, because you've forced it to target you instead, but it's also unable to target you, so it's attack is wasted.
In both cases, after you've used Mounted Combatant, the mount is not a valid target, because the feat "force[s] an attack [...] to target you instead". Since this is the same attack, and it's been forced to not attack the mount because of this feat, it cannot be re-targeted, even after sanctuary applies.
CC takes the hit.
The horse is the one provoking the attack of opportunity, no matter who the attack eventually targets.
After the attack of opportunity has been provoked, the troll is simply making a melee attack against a target - and the Mobile feat does not provide you immunity to being targeted by melee attacks.
Some context about opportunity attacks, mounted combat, and the mobile feat:
When riding a mount, you are moved by someone without using your movement, action, or reaction, and as such you do not provoke an opportunity attack.
You also don’t provoke an opportunity attack when you teleport or when someone or something moves you without using your movement, action, or reaction. (Chapter 9, Making an Attack - PHB)
Mounted combat also specifies that an attacker can choose to target you with an opportunity attack even when you don't provoke one, if your mount provokes an opportunity attack.
if the mount provokes an opportunity attack while you’re on it, the attacker can target you or the mount. (Chapter 9, Mounted Combat - PHB)
So in actuality, the Mobile Feat provides no real benefits while mounted:
You are using your mount's speed to move, so the speed increase does not matter;
Your mount would be the one taking the dash action, not you;
You already do not provoke opportunity attacks when mounted.
And aside from that, the discerning rider might want to have his mount Disengage as its action, avoiding this situation entirely.
Best Answer
Choosing a target is the first step of the Attack action and it happens before any roll (see PHB p. 194). Therefore, this needs to be declared before the attack roll.