An accurate scoring function for database search is crucial for peptide identification using tandem mass spectrometry. Although many mathematical models have been proposed to score peptides against tandem mass spectra, our method (called PepHMM, http://msms.cmb.usc.edu) is unique in that it combines information on machine accuracy, mass peak intensity, and correlation among ions into a hidden Markov model (HMM). In addition, we develop a method to calculate statistical significance of the HMM scores. We implement the method and test them on two sets of experimental data generated by two different types of mass spectrometers, and compare the results with MASCOT and SEQUEST. Under the same condition, PepHMM has a much higher accuracy (with 6.5% error rate) than MASCOT (with 17.4% error rate), and covers 43% and 31% more spectra than SEQUEST and MASCOT, respectively.