Why are iPhones locked?

Because of the profit Apple stands to make by maintaining the exclusivity of the deal with AT&T

Amount AT&T pays per iPhone connection in one month                            $18
Amount AT&T pays per iPhone connection in the two year period            $ 432

Money Apple possibly shall lose due to unlocked iPhones so far (250,000X432) : $108 million in the two year period