We suggest that neutron star mergers eject an ultrarelativistic envelope of mass m similar to 10(-7) M-circle dot, which helps explain the gamma-ray burst from GW170817. One ejection mechanism is the ablation of the neutron star surface by the burst of neutrinos in the first 30 mu s of the merger. Another, more efficient mechanism for inflating the ultrarelativistic envelope is an internal shock in the massive ejecta from the merger. A strong shock is expected if the merger product is a magnetar, which emits a centrifugally accelerated wind. The shock propagates outward through the ejecta and accelerates in its outer layers at radii r similar to 10(9)-10(10) cm, launching an ultrarelativistic opaque envelope filled with similar to 10(4) photons per nucleon. The Lorentz factor profile of the envelope rises outward and determines its homologous expansion, which adiabatically cools the trapped photons. Once the magnetar loses its differential rotation and collapses into a black hole, a powerful jet forms. It drives a blast wave into the envelope, chasing its outer layers, and eventually catching up with the envelope photosphere at r similar to 10(12) cm. The ultrarelativistic photospheric breakout of the delayed blast wave emits a gamma-ray burst in a broad solid angle around the merger axis. This model explains the gamma-ray pulse from merger GW170817 with luminosity L-gamma similar to 10(47) erg s(-1), duration Delta t(obs) similar to 0.5 s, and characteristic photon energy similar to 100 keV. The blast-wave Lorentz factor at the envelope photosphere is consistent with Gamma greater than or similar to 5, which we derive from the observed light curve of the burst. We suggest future tests of the model.