<p>When you die with a legend, your soul does not return to Akasha to be recycled, ie. you are removed from the cycle of reincarnation. You are instead sent to the Throne of Heroes.
</p><p>When you make a contract with the world (Alaya), it means you become a Counter Guardian after death, such is the case of Archer. You will also be sent to the Thrones of Heroes.
</p><p>Here's the thing, whenever humanity is threatened, Alaya will summon the Counter Guardians to deal with the threat. Their MAIN BODY is actually summoned, so everything they learn and experience stays. Normally, only Counter Guardians (such as Archer) are sent to fight in such events (and not Heroic Spirits from legend).
</p><p>The Holy Grail, however, cannot summon the main body of these Heroic Spirits. Such a miracle is only possible by Alaya. What the Holy Grail instead does, is preparing a suited vessel (the class) and using information from the original heroic spirit, it summons a COPY of it into the world. You should therefore think of a Servant summoned by the Grail and the true Heroic Spirit as two separate entities. Because of this, it is natural that the true HS does not retain the memories of its copy's summoning. However, when the copy dies and returns, it does actually send information back to the true Heroic Spirit, but that's all it is; unordered knowledge, not actual memories. This was explained by Rin in UBW by something like undated "books or diaries" sent back for the spirit to read.
</p><p>So, in Archer's case, he actually has memories of all the events he goes through whenever Alaya summons him to do his duty as a Counter Guardian. In the Holy Grail War system, however, he is only a copy of the true Archer, but the knowledge will still be accessible to him.
https://www.google.com/amp/s/amp.reddit.com/r/fatestaynight/comments/369a30/spoiler_vn_ubw_question_about_counter_guardians/
</p>