On a modern Linux box, dig and your applications may not use the same path. dig talks to a server directly. Your apps go through systemd-resolved.
resolvectl query asks the local resolver, the same way a program would, and prints which interface and protocol the answer came from and whether it was served from cache.
resolvectl query example.com
The cache note matters. If dig finds a name but a program cannot, run this one. It also resolves names that dig never sees, like single-label names that expand through search domains or names answered by mDNS on the local network. Add --type=MX for another record type.
One caveat: with --type= it switches to a lower level lookup and stops applying search domains. And this only helps on systems that run systemd-resolved, which is the default on current Ubuntu but not everywhere.
For the full picture of which DNS servers a link uses, resolvectl status is the companion. Query tells you what happened to one name, status tells you how the resolver is set up.
Further reading: resolvectl manual, On this site: resolvectl flush-caches clears the DNS cache on Linux, On this site: resolvectl monitor streams every DNS query live