Yes, "to learn a lesson" is "att lära sig en läxa" in Swedish, if it is about learning something from a bad experience. Normally, "läxa" means "homework" though.
Can en lektion be a lesson outside of the classroom? For instance, a martial arts lesson or a lesson for a musical instrument?
A föreläsning is always a lecture as in e.g. a university lecture. A lektion is always a lesson as in e.g. a fifth grade maths class. In English, there's some overlap, but not so much in Swedish.