This is clear in the rules of swimming. For example, take the USA Swimming (official web site) rules, which follow from FINA. In section 102.24, Timing Rules one finds:
102.24 TIMING RULES
Requirements for Official Time
A. Performance Requirements
(1) An official time can be achieved only in USA Swimming sanctioned or USA Swimming approved
competition,
or in an observed swim in accordance with all applicable rules. It may be achieved in:
(a) Any heat
(b) A swim-off
(c) A lead-off leg in a relay
So, it must be a lead off-leg of the relays (such as Ryan Murphy's 100 back time at the 2016 Olympics). The freestyle portion of the medley relay is not the lead off leg, so that would not count. As a general principal, a relay start gives you about a 0.7 second advantage over a flat start (not firm science, just 40+ years of experience).