Short answer: nur has a wide ranges of usages, and lediglich is a synonym for only some of them.
Long story: First of all, there is the particle nur which is used to emphasize a phrase in a certain way (difficult to explain in detail). You cannot use lediglich as a synonym for the particle nur.
Then there is the adverb nur, which itself has several slightly different meanings. The Duden groups its examples (see the link, I don't want to quote the whole page here) by limitation (1), exclusiveness (2) and contradiction (3). The adverb lediglich always expresses a limitation, it makes something "smaller", so it can replace nur in most of the examples in group (1), and since exclusiveness can be also seen as a kind of limitation you might hear phrases like in group (2) with lediglich, though I would not advise it - it feels a little bit wrong.
Let's have a closer look at the Duden's examples in group (1):
das war nur ein Versehen
ich bin auch nur ein Mensch
ich bin nicht krank, nur müde
ich wollte nur sagen, dass
You can use lediglich instead of nur in the 1st and 4th phrase. It would also work in the 3rd phrase, but sound unidiomatic - "nicht / nur" is a fixed pair for expressing a contrast. In the 2nd phrase, nur is bound to auch. It means "I am just a man like everybody else". Replacing only nur with lediglich would sound strange, and replacing auch nur would change the meaning: "Ich bin lediglich ein Mensch" would lack the reference "like everyone else" and instead emphasize that I as a man have limited or unsufficient capabilities.
ich habe nur 10 Euro = I only have 10 Euro
ich habe nur noch 10 Euro = I only have 10 Euro left
sie sind nur mäßig begabt
you can use lediglich in the 1st and 3rd phrase without changing the meaning. It would sound a bit uncommon in the 2nd one and shift the emphasis from left to only.
So to come up with an ad-hoc rule of thumb: lediglich works as a synonym for nur when it expresses that something is small or limited and does not go along with some other adverb or particle (like "auch nur", "nur noch", "nicht / nur").
Last but not least, nur is a very common and frequently used word, whereas lediglich is a bit more elevated in style.