A sufficient condition is that $f:X \rightarrow B$ is flat and locally finite type map between locally noetherian schemes such that its fibers are local complete intersection morphism (see EGA IV$_4$, 19.2 for relative lci maps), in which case $\omega_{X/B}$ is even a line bundle on $X$ whose formation commutes with any base change on $B$.  This covers the case of flat families of semistable curves, for example, and this lci condition on fibers is open on the base.