Don’t panic! You work it in just the same was as if you were going up a side seam, but instead of picking up the “ladders” going up between the stitches you actually work the mattress stitch into the stitches - difficult to explain without pictures but if you hold the two end parallel with each other just like normal, come up through the middle of the first stitch on for example, the cast off end and then down through the middle of the first stitch on the cast on end, come up through the middle of the second stitch on the cast on end and then down through the middle of the seond stitch on your cast off end and so on until the seam has been joined. Just the same was as picking up two rungs of the ladder in side seam mattress stitch.
I don’t know if I’ve explained that very well, but if you look at it with the knitting in front of you, it should make a bit more sense!