I am writing medley from musical and I was wondering if it is necessary to keep all soundtracks in the same key?
It's not necessary. It will work, although the range of each instrument may stop it doing so. It's also not necessary to keep the snippets in the same key as they were in the show. Sometimes, it's good to change key, maybe up a semitone or tone, to add vibrance to the medley. Also when a different tempo and/or feel and/or time sig. happens, a key change may, or may not help. Going from a major tune to a minor one, you could stay in the same parallel key, or move to the relative minor, or use the supertonic of the last key as your new key. For key changes, you could use a reprise of the last four bars, say, of the preceding tune to take things to the next key. Lots of options!
Any bridge/modulation that works in a single piece will also be able to connect parts of a medley.
In some respect, it depends on how long the medley pieces to connect are: some medleys change song about every two lines. Having a bridging modulation as often as that might be exhausting for the listener. So basically you want to keep key changes down to a level that makes for a somewhat coherent listening experience.
If you are planning a Schönberg medley, people will not likely notice key changes anyway.
If you're talking about Broadway-esque tunes, or Gershwin, or that ilk...the answer is that probably the same cast had to sing all those tunes, and so it's a daring composer that does dramatic key shifts on vocalists!
Just bear in mind that when you're constructing a reprise medley, you have to treat the different keys as modulations...meaning you'll likely be stuck with a couple of "glue" measures to accomplish them.