We present a lattice QCD calculation of the up, down, strange and charm quark masses performed using the gauge configurations produced by the European Twisted Mass Collaboration with Nf = 2 + 1 + 1 dynamical quarks, which include in the sea, besides two light mass degenerate quarks, also the strange and charm quarks with masses close to their physical values. The simulations are based on a unitary setup for the two light quarks and on a mixed action approach for the strange and charm quarks. The analysis uses data at three values of the lattice spacing and pion masses in the range 210 - 450 MeV, allowing for accurate continuum limit and controlled chiral extrapolation. The quark mass renormalization is carried out non-perturbatively using the RI-MOM method. The results for the quark masses converted to the bar{MS} scheme are: mud(2 GeV) = 3.70(17) MeV, ms(2 GeV) = 99.6(4.3) MeV and mc(mc) = 1.348(46) GeV. We obtain also the quark mass ratios ms/mud = 26.66(32) and mc/ms = 11.62(16). By studying the mass splitting between the neutral and charged kaons and using available lattice results for the electromagnetic contributions, we evaluate mu/md = 0.470(56), leading to mu = 2.36(24) MeV and md = 5.03(26) MeV.