Why is baptism outside the Church valid, but marriage outside the Church not?
The short answer
Both can be valid outside the Church. But Church law requires Catholics to marry in the Church or with permission, for pastoral and theological reasons that apply to marriage. Those reasons don't apply to baptism, which Jesus willed for everyone, so any baptism done as he instructed is valid.
The rule for Catholics
Church law obliges Catholics to marry in the Church or obtain formal permission to marry elsewhere (CIC 1108).
Why the form exists
The New Commentary on the Code of Canon Law explains that, pastorally, the required form lets the Church's ministers help couples assess their readiness and give them catechesis and preparation; theologically, it ensures the wedding has at least the minimal ecclesial and liturgical character of a sacrament of the Church.
Baptism is different
Jesus willed that everyone be baptized (Matthew 28:19). The concerns about marriage — its public character, readiness, catechesis, preparation, liturgical dimension — do not apply to baptism. So any baptism administered according to Jesus' instructions is valid.