We study the formation of vortices in a Bose-Einstein condensate (BEC) that has been prepared by allowing isolated and independent condensed fragments to merge together. We focus on the experimental setup of Scherer {it et al.} [Phys. Rev. Lett. {bf 98}, 110402 (2007)], where three BECs are created in a magnetic trap that is segmented into three regions by a repulsive optical potential; the BECs merge together as the optical potential is removed. First, we study the two-dimensional case, in particular we examine the effects of the relative phases of the different fragments and the removal rate of the optical potential on the vortex formation. We find that many vortices are created by instant removal of the optical potential regardless of relative phases, and that fewer vortices are created if the intensity of the optical potential is gradually ramped down and the condensed fragments gradually merge. In all cases, self-annihilation of vortices of opposite charge is observed. We also find that for sufficiently long barrier ramp times, the initial relative phases between the fragments leave a clear imprint on the resulting topological configuration. Finally, we study the three-dimensional system and the formation of vortex lines and vortex rings due to the merger of the BEC fragments; our results illustrate how the relevant vorticity is manifested for appropriate phase differences, as well as how it may be masked by the planar projections observed experimentally.