In most countries, yes. In fact, many of the world’s largest lotteries officially allow their countries’ residents to purchase tickets online directly from the official lottery site.
However, some of them do not allow foreigners to do so unless they are physically within the country at the time they buy their tickets. In these cases, third-party lottery sites are the only way that foreigners can either buy physical tickets for the draws or bet on the results of the draws without having to travel anywhere.
Since lotto agents are providing a concierge service to buy tickets on your behalf and not reselling tickets that they had previously purchased themselves (something that is not permitted), they are 100% legal.
Furthermore, lottery betting sites have nothing to do with the official lotteries themselves other than using them as a basis for what results people are betting on. Therefore, as long as they are licensed to operate within the jurisdictions they exist in, they are completely legal too.
That being said, there are some countries, including the US and China, that do not permit online gambling. If you’re not sure what your country’s stance on this is, check our list of countries that have banned online gambling.