In mathematical and other theoretical fields, there is not much need for expensive equipment, so grant money is mostly used to fund students and research assistants. But in some fields, good students and assistants are very hard to find (for example, in computer science, the salary in the industry is so high, that it is nearly impossible to beat with grant money). In such cases, apparently the most efficient way to use the money is to reduce teaching load from the principal investigators (e.g. by paying external teachers to teach the basic courses), so that they can spend more time on their research. Is there any fund that allows to use the money in such a way?
I'm sure that this varies from country to country, but I think a lot of grants (and universities) will let a researcher "buy out" of some sorts of teaching using grant funds. Fewer will let you buy out of all teaching, since that would defeat one of the main missions of a university. But, buying out of an undergraduate class might be possible.
I'd guess that long term buy outs from teaching would be very rare, only for superstars, and not applicable to doctoral advising, or maybe even advanced specialty courses.
And, in some places, doctoral advising counts against the teaching load also, but not one student per class.
Also note that it isn't just up to the grant provider. The university also has to judge that this is a worthwhile tradeoff.
Here is a policy at one university:
And here is a program that doesn't rule out course reduction necessarily:
Note, also, that there are other reasons a university might grant a reduced load than buyouts.
It is entirely possible but (as you can imagine) such grants are not so easy to get. Basically, it depends on the rules of the grant.
There are some “non-traditional” grants that are designed for this: basically the amount of the grant pays for buyouts and the scholar must produce some report at the end of the grant periods. Some think tanks contract academics in this way.
It is not that uncommon for institutes to have core members with no defined teaching duties: they can teach courses (usually graduate courses) when they want on the topic of their choices. In physics the Perimeter Institute in Waterloo operates this way: its core members are affiliated with the university (and thus can supervise students) but do not operate as regular faculty members (although this situation is not strictly a grant, but a perk of appointment there).
There are also exceptional circumstances: endowed chairs for instance might come with full or partial teaching buyouts.
It goes without saying that such grants or positions are highly competitive.
Indeed this depends on the country. The answer is mostly that Europe has grants like this moreso than North America in my experience. I don't know about other continents so I will focus on what I know below:
In the USA, the way grants from the NSF often work is through providing summer salary. I haven't seen much there.
On the other hand, UK Research and Innovation grants usually pay for a certain percentage of the academic staff member's salary in order to pay for their time to execute the grant. Typically this number is somewhere around 20% for a standard grant for a permanent faculty member (so a day a week reserved for the grant). One prestigious style of UK grant is the fellowship. In the UK, fellowships will fund somewhere between 75-100% of the researcher's stipend, with some having explicit maximum requirements of time used off the grant (e.g., only x hours a week can be used on academic activities such as teaching and admin off the grant).
European Research Council Starting / Consolidator grants often fund sizeable portions of the PI's time as well. Advanced Grants less so as they expect it is harder for the PI to get out of certain obligations as a senior member of a department.