To eventually become a mathematics lecturer in the UK, does undergraduate degree have to be from a good university?
First, let me point out that a career as you describe it not entirely plannable: There are many more who want it than who get it, and even if you are talented and work hard, success is not guaranteed.

It probably won't matter where you did your undergrad once you've received your PhD, although going to different universities could be beneficial, as it would expose you to more different academic cultures etc.

Once you have received your PhD, you'll want to do a good postdoc. Going to some top university abroad, or becoming a Junior Research Fellow in Cambridge or Oxford could be good choices. Make sure that you develop contacts at your desired university.

After some postdoc experience you can apply for eg a Royal Society Research Fellowship or an EPSRC Early Career Fellowship, to be held at your desired university. During that time, try to convince the university to offer you a permament position after the fellowship ends.