The powerful muon and tracker systems of the CMS detector together with dedicated reconstruction software allow precise and efficient measurement of muon tracks originating from proton-proton collisions. The standard muon reconstruction algorithms, however, are inadequate to deal with muons that do not originate from collisions. We present the design, implementation, and performance of a dedicated cosmic muon track reconstruction algorithm, which features pattern recognition optimized for muons that are not coming from the interaction point, i.e. cosmic muons and beam-halo muons. To evaluate the performance of the new algorithm, data taken during Cosmic Challenge phases I and II as well as beam-halo muons recorded during the first LHC beam operation were studied. In addition, a variety of more general topologies of cosmic muons and beam-halo muons were studied using simulated data to demonstrate some key features of the new algorithm.