In this paper, a novel Markov random field (MRF)-based approach is presented for segmenting medical images while simultaneously registering an atlas nonrigidly. In the literature, both segmentation and registration have been studied extensively. For applications that involve both, such as segmentation via atlas-based registration, earlier studies proposed addressing these problems iteratively by feeding the output of each to initialize the other. This scheme, however, cannot guarantee an optimal solution for the combined task at hand, since these two individual problems are then treated separately. In this paper, we formulate simultaneous registration and segmentation (SRS) as a maximum a-posteriori (MAP) problem. We decompose the resulting probabilities such that the MAP inference can be done using MRFs. An efficient hierarchical implementation is employed, allowing coarse-to-fine registration while estimating segmentation at pixel level. The method is evaluated on two clinical data sets: 1) mandibular bone segmentation in 3D CT and 2) corpus callosum segmentation in 2D midsaggital slices of brain MRI. A video tracking example is also given. Our implementation allows us to directly compare the proposed method with the individual segmentation/registration and the iterative approach using the exact same potential functions. In a leave-one-out evaluation, SRS demonstrated more accurate results in terms of dice overlap and surface distance metrics for both data sets. We also show quantitatively that the SRS method is less sensitive to the errors in the registration as opposed to the iterative approach.