Please do not be afraid - but, yes - you probably just coughed up a parasite. It occasionally does happen as some species of parasite incubate and hatch out in the lungs - they are then coughed up an swallowed in phelm - where they travel to the intestines to continue their life cycles.
You just happen to "catch" one - it was bigger than most and that is probably why he got caught.