Can Congress essentially pardon a violation of law through
legislation?
Yes.
Congress has the power to retroactivity reduce the sentence for a crime for which someone has been sentenced. This was done most recently in the Fair Sentencing Act of 2010 that reduced excessive penalties for crack cocaine relative to powder cocaine.
In the same way, when the death penalty is legislatively repealed in a state, the death sentences of the handful of people on death row at the time is often commuted legislatively.
While Congress cannot impose criminal penalties on someone legislatively, which is a Bill of Attainder and constitutionally prohibited, it can single out people for special treatment in a private bill, which is a constitutional exercise of legislative power at the federal level (not every state allows private bills to be enacted due to Progressive era reforms to state constitutions).
For example, private bills often eliminate the collateral effects of a criminal conviction upon a person, there is even a standard procedure for doing so, which is functionally equivalent to a Presidential pardon of the crime for that purposes (the vast majority of Presidential pardons are issued after the person convicted has served the sentence for the crime).
A private bill cannot impair contract or property rights, which would be a prohibited ex post facto law.
But no one other than the federal government has a legally protected interest in keeping someone incarcerated or otherwise punishing them for a crime. Crimes are prosecuted in the name of the People and victims of crimes do not have legal rights in those proceedings except as created by statute. So, this would not be an ex post facto bill or a taking governed by the Fifth Amendment for takings of property interests. And, Congress may, by legislation, determine what the federal government will do in essentially all cases where it is not expressly prohibited from doing so (some argue that there is a minimum of federal authority vested exclusively in the President in the area of foreign affairs and military affairs, but that exclusivity of power does not extend to domestic criminal justice).
Basically, anything that Congress could do for everyone via a public bill, it can do for someone in particular via a private bill, unless a specific constitutional prohibition applies, and there is no such prohibition when it comes to relieving someone from a sentence or the collateral effects of a criminal conviction prospectively via a private bill. This isn't exactly equivalent to a Presidential pardon or commutation, but it is very close to one in practical effect.
For a fuller, but somewhat outdated treatment of the issue, you can read this 1939 article in the California Law Review which acknowledged that legislative pardons were possible under existing law.
This said, government prosecutors routinely fervently oppose any retroactive criminal legislation that reduces punishment, particularly via private laws (although some countries, such as France, have constitutional requirements to retroactively reduce the sentences of anyone currently serving time for a crime whose punishment is legislatively reduced prospectively).
Private bills that constitute legislative pardons are very rare. A 2011 law review article recounts how the tool of the legislative pardon (in parallel with the executive pardon which is also used much less frequency) has fallen into disuse.
Part of the decline is due to the adoption of the right to appeal from a trial court criminal convictions which did not exist in the federal system until the 1890s. Until then, the only judicial relief available from a federal criminal conviction was via a writ of habeas corpus, and that was available only on very limited grounds such as a lack of jurisdiction to conduct the trial, or the non-existence of the crime of conviction. Conviction by a jury in a court with jurisdiction of a constitutionally permissible crime was an absolute defense to the extent of the sentence imposed to a habeas corpus petition at that time.
(As a footnote, the term "private bill" can be confusing. In many governments with a parliamentary system, a "private bill" means one sponsored by an individual legislator rather than the prime minister and his or her cabinet. But, in U.S. terminology, a "private bill" refers to a bill with an effect limited to one person or a small number of persons who are either identified by name or by a very narrowly defined situation.)