The unicorn was banished to Shrek's swamp by Lord Farquaad. He was sold by Alice from Alice's Adventures in Wonderland.
However, he made his debut in the novel's sequel, Through the Looking-Glass and What Alice Found There. Thus, he knew Alice from the events of that adventure in her life.
Community content is available under CC-BY-SA
unless otherwise noted.