Medieval arrow makers understood the principle of making an arrow shaft rotate around its axis in flight to stabilise it.
Generally, the fletchings on an arrow are slightly offset to achieve this.
On that basis, it is not surprising that someone would try to apply that principle to guns.
A bullet wrapped in a patch of cloth so as to fit the bore of a gun tightly gives much better accuracy than one that is loose.
This created the idea that a bullet would be more accurate if it could be in contact with the barrel walls as it passed down the bore.
Rifling was probably invented by mistake.
Some guns had straight grooves cut in their barrels to collect fouling and it may be that someone put a slight twist on these grooves to make them longer and more efficient.
The resultant improvement in accuracy was a welcome side-effect.
The earliest gun with a rifled barrel of which I am aware is a matchlock harquebus made for the Emperor Maximilian I between 1493 and 1508.
Its barrel is bronze and though the bore is badly corroded, there are definite traces of rifling.
Small numbers of effective rifled guns were known in the 16th and 17th centuries, but their general use for hunting and warfare did not come about until the beginning of the 18th century.