The US did not pull out of Afghanistan under Obama.
But especially in this specific case, where he actually presided over a dramatic US escalation in Afghanistan:
https://www.afghanistanwarcommission.senate.gov/press-releas...
in 2008 the American public was deeply upset about how things were going in Iraq. Obama was one of the only 2 senators voting against the war in Iraq back in 2003; and Hillary Clinton voted for the war in Iraq, while McCain vowed to be aWar president. In this climate Obama won both the Democratic nomination and later the US presidency.