A few days ago, my site was affected by malware. There were lot of URLs that linked to my site and the malware used to redirect to some place. I have gotten rid of all the malware, but still there are a lot of URLs that appear as, linked to my website and is being reported as 404 errors. How do I get rid of those, so that I stop getting these 404 errors in my webmaster tool.
Unfortunately there's not much you can do. You can't prevent these other (hacked) sites from linking to you. Besides, once Google has found these (now erroneous) URLs, it is likely to continue crawling them for sometime to come, even when the inbound link is (eventually) removed.
You are already doing the correct thing, in returning a 404. A genuine 404 is not a bad thing, but (as you have found) it is annoying that they unnecessarily pollute your reports.
You can try returning a 410 Gone instead, but if the URLs aren't actually indexed anyway then it probably won't make that much of change to the GSC report (at least initially).
You may also want to disavow these links.
It could potentially appear like you purchased links from a garbage SEO provider helping to sell you links to increase your rankings. This behavior gets penalized commonly, and you'll need to disavow those links to Google knows that you were weren't hoping to get any ranking benefit from the added backlinks.
You can find the links you need to disavow a variety of ways (Google's Search Console, Majestic, Ahrefs, and OpenSiteExplorer.org are some sites that come to mind). And then go into Google's Search Console in your webmaster account and open up the disavow tool. Remember, if it's ultra spammy stuff, you'll likely want to disavow from the domain level and not just a specific URL.
Probably you will find the procedure in here:
This is a two-step process. First, you’ll need to download a list of links to your site. Next, you’ll create a file containing only the links you want to disavow and upload this to Google.
Download links to your site
Choose the site you want on the Search Console home page. On the Dashboard, click Search Traffic, and then click Links to Your Site. Under Who links the most, click More. Click Download more sample links. If you click Download latest links, you'll see dates as well.
Note: When looking at the links to your site in Search Console, you may want to verify both the www and the non-www version of your domain in your Search Console account. To Google, these are entirely different sites. Take a look at the data for both sites. More information
You’ll download a file containing all the pages linking to your site. Use this to create a text file (the file type must be .txt and it must be encoded in UTF-8 or 7-bit ASCII) containing only the links you want to disavow—one link per line. If you want Google to ignore all links from an entire domain (like example.com), add the line "domain:example.com". Your text file can include additional information about excluded links, as long as each line of description begins with the "#" character (all lines beginning with # will be ignored). Don't upload the entire list of links to your site: the text file that you upload is the list of links you want Google to ignore.
Here's a sample of a valid file
-Contacted owner of shadyseo.com on 7/1/2012 to -ask for link removal but got no response domain:shadyseo.com
If you want Google to ignore all links from an entire domain (like example.com), add the line domain:example.com.
Upload a list of links to disavow
Go to the disavow links tool page. Select your website. Click Disavow links. Click Choose file.
# Note: Uploading a new file will replace all previously uploaded ones.
It may take some time for Google to process the information you’ve uploaded. In particular, this information will be incorporated into our index as we recrawl the web and reprocess the pages that we see, which can take a number of weeks. These links will continue to be shown in the Search Console inbound links section.